# Questions tagged [time-limit-exceeded]

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.

97 questions
Filter by
Sorted by
Tagged with
2answers
624 views

### Prime generator for SPOJ

I have written code to solve the prime generator problem. I am given an input which contains in the first line the number of test cases, and for each test case, I am given a range and I have to print ...
3answers
737 views

### LeetCode 49: Group Anagrams

I'm exceeding the time limit for a 10,000 word test case provided on LeetCode: Given an array of strings, group anagrams together. For example, given: ...
17answers
18k views

### Disproving Euler proposition by brute force in C

I wrote a little bit of code to brute force disprove the Euler proposition that states: $$a^4 + b^4 + c^4 = d^4$$ has no solution when $a$, $b$, $c$, $d$ are positive integers. I'm no ...
4answers
12k views

### Finding the first duplicate in an array

My code returns the correct answer the first duplicate is 3 (represented as int f) . I am struggling finding a more efficient way to find the first duplicate? My ...
4answers
5k views

### Hacker Rank - Poisonous Plants

I have the solution for the problem, but it is taking me 7 seconds to run on a large dataset. I am trying figure out a better way of doing this. It has to run in under 3 seconds. Problem Statement: ...
5answers
5k views

### Euler #3 largest prime factor

Problem Statement This problem is a programming version of Problem 3 from projecteuler.net The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of a given number N?...
2answers
3k views

### O PC, why are you so slow?

In order to test some software I've written, I need to write values to a test harness with OLE for Process Control (OPC). The problem is, for a larger range of values I'm generating points for (say ...
1answer
5k views

### BFS shortest path for Google Foobar challenge “Prepare the Bunnies' Escape”

This is the Google Foobar challenge "Prepare the Bunnies' Escape": You have maps of parts of the space station, each starting at a prison exit and ending at the door to an escape pod. The map is ...
1answer
5k views

### BFS shortest path for Google Foobar “Prepare the Bunnies' Escape”

This is the Google Foobar puzzle "Prepare the Bunnies' Escape": You have maps of parts of the space station, each starting at a prison exit and ending at the door to an escape pod. The map is ...
2answers
650 views

### Printing all the prime numbers between two bounds

I keep getting a time limit exceeded for this solution to SPOJ Prime1. I need to print all primes within a given interval (with intervals as large as 105, and numbers as large as 109). I've tried ...
2answers
6k views

### Codewars: Sum of Pairs

I've been working on this task (see screenshot). Here's my answer to it so far: ...
1answer
278 views

### Counting ways to fit bottles in crates

I'm calculating how many possibilities there are to fit $n$ identical bottles inside $k$ crates of various capacities. n = Number of bottles k = Number of crates K = List of the number of bottles ...
1answer
12k views

### Shortest Path For Google Foobar (Prepare The Bunnies Escape)

I have been working on Google Foobar since a few days ago. I am currently in the third level but is stuck in the second challenge of "Prepare the Bunnies' Escape." I have checked this post but it did ...
4answers
2k views

### Codechef: Prime Number Generator

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input The input begins with the number t of test cases ...
3answers
16k 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. ...
4answers
2k views

### A faster way to compute the largest prime factor

I am self-learning js and came across this problem(#3) from the Euler Project The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? ...
4answers
3k views

### Leetcode 56: Merge Intervals

Problem statement Given a collection of intervals, merge all overlapping intervals. For example: Given $[1,3],[2,6],[8,10],[15,18]$, return $[1,6],[8,10],[15,18]$. My ...
2answers
5k views

### Google FooBar XOR Checksum Challenge

Google FooBar came up a few days ago and I took it as a challenge to learn python quickly while having fun. However, I ran into one challenge today that has left me stumped, I've come up with a ...
3answers
3k views

### Calculate highest prime factor very slow

Sometimes when I write a Python 3.x script with too many nested loops and some big values it just takes too long to give the answer. How can I optimize my code so that it runs faster, or where should ...
4answers
3k views

### 2-sum problem on a range in Python

Here is the problem description (from an algorithm course: https://www.coursera.org/learn/algorithm-design-analysis): The goal of this problem is to implement a variant of the 2-SUM algorithm (...
5answers
8k views

### “Algorithmic Crush” problem hitting timeout errors

This is the HackerRank problem Algorithmic Crush: You are given a list of size N, initialized with zeroes. You have to perform ...
1answer
176 views

### Using bisect to flip coins

I am trying to solve this problem on Codechef, which has a list of $N$ coins, all initially tails-up, and a list of $Q$ commands. There are two possible commands, ...
4answers
1k views

### Project Euler problem 14 (longest Collatz sequence) in Swift 3

I was trying to solve Project Euler:Problem 14 using Swift 3, but it takes ages to give me an answer, which is a sign that my code is absolute garbage performance-wise. What could I do to increase the ...
1answer
1k views

### Python program to find a word ladder transforming “four” to “five”

I saw this Puzzling problem and thought I would try to write a Python program to solve it. The task is to transform "four" to "five", forming a new four-letter word at each step, replacing one letter ...
4answers
1k views

### Sum of digits in a^b

I'm trying to calculate a problem which requires sum of digits in $a^b$, where $0 \le a \le 9$ and $0 \le b \le 4000$. I have seen various other posts similar to this, but I haven't acquired ...
2answers
1k views

### Fastest path on a snakes and ladders board

I am not looking for a detailed review of this code as I am aware it is very inefficient. The main reason for posting this is to see if there is a better way to achieve my goal (I am certain there ...
3answers
2k views

### Determine Collatz sequence length for given starting values

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 ...
5answers
3k views

### Flipping coins performance

This is the Flipping coins problem on CodeChef: There are $N$ coins kept on the table, numbered from $0$ to $N - 1$. Initially, each coin is kept tails up. You have to perform two types of ...
1answer
5k views

### Hackerrank Queen's Attack II

Problem statement A queen is standing on an $n$x$n$ chessboard. The chessboard's rows are numbered from $1$ to $n$, going from bottom to top; its columns are numbered from $1$ to $n$, ...
3answers
6k views

### Finding indices of numbers that sum to a target

I've taken this algorithm question, which is to find the indexes of any two numbers in an array that sum to a given number. It is quite a simple problem to solve. But it is the execution time of my ...
2answers
145 views

### Prime number segmented sieve

I tried to solve the SPOJ prime number problem below. The code produced correct results, but Time limit exceeded and also memory need around 970M which is not efficient(?) How can I optimize this ...
4answers
10k views

### Summing Divisors

Definition: A proper divisor of a natural number is the divisor that is strictly less than the number. e.g. number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation is: 1 + ...
2answers
149 views

### A recursive program that attempts to solve an extremely large number

This is my recursive program for the following function: $F(x) = G(x) - W(x)$ $G(x) = [G(x-1)+G(x-2)]^2$ $W(x) = [W(x-1)]^2 + [W(x-2)]^2$ If x == 0 in either function G or function W, then ...
3answers
1k views

### Project Euler 92: Square digit chains

A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before. For example, 44 → 32 → 13 → 10 → 1 → 1 85 → ...
6answers
7k views

### Project Euler #10 — Summation of primes takes forever

I have resolved the Problem presented in Project Euler #10: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. But this code takes too long ...
2answers
123 views

### Estimating the number of tanks based on a sample of serial numbers

I created code that was supposed to determine how well my system of guessing is. The scenario is that there are n amount of tanks. Each tank is uniquely numbered 1 to n. Seven of these tanks are ...
2answers
111 views

### Matching maximal nodes with velocities

I have written a code which works very good for small data-files (see below). The data file 'WSPL.dat' which I want to evaluate has a size of 2.6 GB and the data file 'VELOC.dat' has a size of 4.3 GB....
1answer
138 views

### Find the number of substrings of a numerical string greater than a given num string

Question: http://qa.geeksforgeeks.org/9744/vinay-queried-interesting-problem-optimization Input The first line contains N denoting the length of Target String. This line is followed by the ...
3answers
341 views

### Estimating the number of tanks based on a sample of serial numbers 2.0

There was some confusion with the code I had posted in my previous version of this question and there was some good advice from @Oscar Smith. The explanation is the same; please give improvements for ...
1answer
410 views

### Processing data for five weather stations

Problem The main idea is to build some fine scale weather data using inverse distance weighting (idw) from the 5 closest stations of the observed station. If the ...
0answers
602 views

### Hackerearth Simple Function - Follow-up

Problem statement Let us first describe a SimpleFunction: ...
1answer
4k views

### Google Foobar Level 3 - Lucky Triples (Find the Access Codes)

I have been working on this problem for 3 days now. I wrote a piece of java code that works perfectly on my machine testing in Eclipse, but when I put it in Foobar, it either returns "Error(400) Bad ...
1answer
846 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: ...
2answers
338 views

### Code for calculating the books dispatched

Recently I came across this, here is an excerpt from it, This is another problem about Indraneel's library. His library has one long shelf. His books are numbered and he identifies the books by ...
3answers
395 views

### Kattis Challenge - Animal Classification

I'm new to competitive programming and C++ in general. I have solved the Kattis Animal Classification challenge. We are given two classification trees, each represented using nested parentheses. ...
1answer
348 views

### Daily SQL job to delete records older than 24 hours

Below is a delete job to delete 1M+ records daily and takes 13 hours to complete and sometimes more than that. I need to optimize this. The table tblcalldatastore is being inserted 24*7 through a ...
3answers
2k views

### N-puzzle solver using A* with Manhattan + Linear Conflict

I've been working on this solution for a couple days now. Finally managed to get it to work and I got it to where it solves about half of the time. The other half goes for so long that it eventually ...
1answer
80 views

### Can phrase be made from whole words in another string?

I am working to solve this HackerRank puzzle: Hash Tables: Ransom Note The premise is that if I am given a string like this: give one grand today I can find a match for each of the words in the ...
2answers
2k views

### Speedup for Project Euler 44 - pentagon numbers

I'm trying to solve the problems of Project Euler with Haskell within less than 10s with optimized code (ghc -O2). Unfortunately, I'm struggling to find a correct ...
2answers
857 views

### Project Euler #12 runs very slowly

The problem is : The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: ...