Tagged Questions
When the code scales so poorly in the face of large inputs that it cannot complete in a reasonable amount of time, use this instead of the [performance] tag.
6
votes
2answers
63 views
HackerRank Sherlock and Squares: count perfect squares in an interval
I've exceeded the time limit on Sherlock and Squares on hackerrank.com.
Problem
Given the number of test cases and a range (inclusive), find the number of numbers within that range that are perfect ...
5
votes
5answers
145 views
Counting pairs of relatively prime numbers
Problem from Hacker Earth:
Inverted GCD:
Given an array a of \$N\$ numbers , you have to find the number of pair of indices \$i\$ and \$j\$ that satisfy the following relation:
\$i &...
2
votes
0answers
29 views
SPOJ GENERAL: sorting by swaps of distance k
I have been trying to solve this simple problem on SPOJ for quite some time now, but I keep on getting TLE (Time limit exceeded) for some reason.
Since the problem is in Portuguese, a brief ...
9
votes
3answers
690 views
Binary search in C#
The challenge requires the program to receive data from the console and use binary search to find if a term is contained in a set of other terms. I've unit-tested the solution below, but according to ...
10
votes
3answers
2k views
Counting permutations without repetitions for a number or a string
Can I make the following code faster with minor modifications? It times out on CodeWars website.
...
3
votes
3answers
149 views
Sorting and eliminating duplicates
From Hacker Earth:
Problem Statement:
Chotu's father is the owner of a Vada Pav shop. One Sunday, his father takes him to the shop. Father tells him that at the end of the day, Chotu has to ...
2
votes
1answer
98 views
Find the number of possible infinite cycles that Bessie the Cow can get stuck in
Bessie the cow is in Farmer John's field. The field is of size 0..1,000,000,000 for x and y. Farmer John knows that there are N/2 wormholes at certain coordinates, but he does not know which wormholes ...
10
votes
3answers
722 views
Macro that does some kind of duplicate check
This macro pulls in two workbooks, one being a template with saved formulas already, and the other containing data with thousands of rows...I need to increase the speed because the process takes more ...
0
votes
0answers
40 views
Time limit exceeded for solving longest palindromic substring by dynamic programming
I have seen plenty of longest common substring search code by dynamic programming and it is an O(n^2) operation. My motivation is to solve leetcode problem longest-palindromic-substring by this ...
3
votes
1answer
83 views
HackerRank “Save Humanity”
The "Save Humanity" problem on Hackerrank asks us to:
... find all substrings in the patient DNA that either exactly matches the virus DNA, or has at most one mismatch.
For example: ...
4
votes
3answers
570 views
Project Euler 10: find the sum of all the primes below two million
This is question #10 from Project Euler:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million.
I just started programming and read that a Sieve ...
3
votes
3answers
96 views
Find All the Primes Between two Numbers
I wrote a solution to the Coding Challenge SPOJ Prime in C++ using the Sieve of Eratosthenes. How much Ever I tried I could not get over the Time Limit on SPOJ which is 6s for Primes upto 1000000000.
...
5
votes
1answer
100 views
Evaluating a series of Legendre polynomials
The following function represents the electrostatic potential, in spherical coordinates, due to a ring of charge \$q=1\$ and radius \$R=1\$, placed in the plane \$x\$-\$y\$:
$$\phi(r,\theta) = \sum_{...
3
votes
3answers
867 views
Making as many unique strings as possible by removing two characters
I'm attempting a programming challenge type task in C#, where the goal is to determine how many unique strings can be obtained by removing two characters. The prompt for the task implied that I should ...
5
votes
3answers
34 views
Counting GUAG introns in chromosomes
I have this code that is working fine but it's taking pretty much 100% of my cpu to run and it takes around 25min. I'd really like to optimize it but don't know what parts I could improve.
The main ...
4
votes
1answer
67 views
+100
Migrate files from MySQL BLOBs to PostgreSQL largeobjects
As title says, this piece of code migrates files (binary and metadata) from a database to another one with different structure.
Currently my problem is that when I have to deal with a big database (...
2
votes
3answers
147 views
Calculate the sum of all primes less than 2,000,000 in Swift
This is a Swift program I wrote to calculate the sum of primes below 2 million, but it is tediously slow.
I am curious about what makes it so slow. My theory is that copying the filtered array is ...
4
votes
1answer
96 views
Finding the similarity between the two movies using Pearson correlation coefficient
I am trying to find the similarity between the two movies using Pearson correlation coefficient. The programs is working well for small inputs but for large inputs (like 100000 lines) it takes forever....
0
votes
1answer
49 views
Knight's tour code
Another one for some late night snack. - Knight's tour!
Please provide your suggestions/flaws/optimization to this knight's tour backtracking code..
...
0
votes
1answer
48 views
Partitions of a number (backtracking) - time efficiency
I had to write an algorithm that prints the partitions of a natural number in lexicographic order. Example of I/O:
If the algorithm reads from ...
4
votes
1answer
120 views
Number of divisors of a factorial
I was solving the DIVFACT problem from Sphere Online Judge:
Given a number, find the total number of divisors of the factorial of the number.
Since the answer can be very large, print the answer ...
3
votes
1answer
170 views
Project Euler #549: Divisibility of factorials
This is the problem:
Calculate
$$\sum_{i=2}^{10^8} s(i)$$
where \$s(n)\$ is the smallest \$m\$ such that \$n\$ divides \$m!\$.
Quite mathematical, I've found a better way than brute ...
1
vote
1answer
67 views
CUDA brute force 48 bit key
I have a cryptographic function with two 24 bit keys.
I have two blocks of input and two blocks of output, and want to brute force the keys using CUDA.
Overview:
The function is composed to two ...
1
vote
0answers
24 views
Python html Page reporter
My program, page_reporter, attempts to port scan a network and identify servers hosting web pages. From here it will create an HTML report using iframes to give you a snapshot of what the page looks ...
2
votes
1answer
111 views
Primes & Squares
Codeforces #230B
Question
Whether a number has exactly three factors. (i.e. 1, itself and its square root, thus squares of primes) [Time limit is 2 sec, Memory 256 MB]
Input
Number of testcases, n ...
3
votes
1answer
74 views
Finding the maximum distance from the starting node
I've tried everything to make my program faster but with 250 nodes my code takes around 9 seconds to print the result, and with 5000 nodes it took around 260 seconds.
Is there a way to make my ...
2
votes
0answers
47 views
1
vote
2answers
60 views
Counting pairs of integers which have a predefined difference
I'm doing this HackerRank problem:
Given \$N\$ integers, count the number of pairs of integers whose difference is \$K\$.
So far, I've ended up with this code:
...
-2
votes
3answers
121 views
Generating a very long random string of letters
I was trying to fill a file with about ten million random characters (a-z,A-Z,' '). To my surprise the code is extremely slow. I inserted this print ...
3
votes
0answers
63 views
Time Limit Exceeded with shortest path algorithm
The problem:
SHPATH
You are given a list of cities. Each direct connection between two
cities has its transportation cost (an integer bigger than 0). The
goal is to find the paths of ...
1
vote
0answers
215 views
SPOJ DIVSUM - DIvisor Summation
DIVSUM - Divisor Summation
#number-theory
Given a natural number n (1 <= n <= 500000), please output the summation of all its proper divisors.
Definition: A proper divisor of a natural ...
3
votes
2answers
104 views
Copying lists into columns of a table
Edit: I found a potential solution. Changing all of the Dim as Long to Dim as Integer allows the scripts to run smoother. ...
4
votes
4answers
371 views
Copying orders and prices into another Excel sheet
I would like to know if the below VBA can be streamlined to process faster, since it takes on average 9hrs to complete a sheet (800 000 lines), and I have quite a few to get through. Running on 3 ...
1
vote
1answer
50 views
Google Code Jam Google String problem: a sequence resulting from bit flips and reversals
I'm currently having a go at some past Google Code Jam problems to help me practice my programming skills.
One of these problems is Problem A ("Googol String") from Round A APAC Test 2016. Here is ...
0
votes
1answer
54 views
Effective ways to find partial sha1 collision
I need to find 2 different string and compare their hash value. Both string must contain "abc". I looking for the first eight character that are the same and stop. I have been running for more than 24 ...
-2
votes
3answers
62 views
Calculating the max difference between any 2 numbers from an array
How can I make this little program work a little faster?
The task is to calculate the max difference between any 2 numbers from an array of numbers. Target time is 1 sec, now it works in 1.1 sec.
I'...
1
vote
1answer
69 views
Prime generator SPOJ problem in Python 3
I am trying to solve an SPOJ problem: print all prime numbers in a given range (as large as \$10^9\$). I have used the Sieve of Eratosthenes algorithm. But it is still slow when input is in range of \$...
1
vote
2answers
42 views
SPOJ “TESSER” - Getting TLE using KMP algorithm
The TESSER - Finding the Tesserect problem is tagged as a problem to be solved by Knuth-Morris-Pratt (KMP) algorithm. I'm using KMP, but am still getting time-limit-exceeded.
Task
Each of T test ...
1
vote
3answers
92 views
3n+1 problem rejected by uva judge: time limit exceeded
I am trying to get into the habit of solving programming puzzles in my spare time but am stuck on the first one(which I know can be for a large number of reasons hence my posting for help). Shown ...
3
votes
2answers
154 views
High execution time of LCS length program in Python2
I was trying to solve the Longest Common Subsequence problem on a HackerRank exercise.
It's quite simple and straightforward, just print out the length of the LCS. I submitted this code:
...
2
votes
1answer
86 views
Swift solution to Leetcode “Longest Substring Without Repeating Characters”
From LeetCode medium 3. Longest Substring Without Repeating Characters:
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", ...
1
vote
0answers
36 views
Union Find implementation
I am trying to complete this challenge. The user should enter a sequence of instructions, = to link two numbers, and ? to query ...
3
votes
1answer
67 views
Chopsticks Game in Python (Version 2)
This is another iteration of my previous question.
This program plays the game Chopsticks using the Minimax algorithm. I search the tree using recursion. The problem, however, is that it is just ...
2
votes
1answer
65 views
Finding the total time elapsed in the union of time intervals
Given a set of time intervals, I have to find the total time elapsed in the union of the intervals.
Test Cases:
...
7
votes
1answer
127 views
Chopsticks game in Python
As a beginning project, I decided to code something that plays the game Chopsticks in Python 3. I'm using the Minimax algorithm, and exploring the tree using simple recursion. One of the main problems ...
3
votes
2answers
36 views
Time Limit Exceeded for build_max_heap
This problem is from HackerEarth:
The Monk learned about priority queues recently and asked his teacher for an interesting problem. So his teacher came up with a simple problem. He now has an ...
1
vote
2answers
54 views
Matching lines in a CSV file with items in a dictionary
I was wondering if there was a better way of writing this...
I have a CSV file (about 6640 lines). After reading through the file, the values for the DFN_Network ...
3
votes
0answers
34 views
2
votes
2answers
295 views
4
votes
2answers
58 views
Number of ways to make change for an amount
Task
Write a program that, given the amount to make change for and a list of coins prints out how many different ways you can make change from the coins to STDOUT.
My approach
The number to make ...