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.

Filter by
Sorted by
Tagged with
7
votes
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 ...
7
votes
3answers
522 views

Finding dictionary keys whose values are duplicates

I currently have a dictionary (Duplicate_combos) that has a unique identifying number for the key value and the value is a list with two elements, a company code ...
7
votes
2answers
1k views

CodeFights: Frisbees

Description You and your friends are seizing another summer day, passing around the frisbee in the park. You have the most fun when the game is fair, so as much as possible, you would all like to ...
7
votes
2answers
442 views

“Ease the Array” challenge

I'm working on this challenge: Given an array of integers of size N. Assume ‘0’ as invalid number and all other as valid number. Write a program that modifies the array in such a way that if next ...
7
votes
4answers
7k views

Calculate the number of palindrome numbers in the given ranges

I've written a program that calculates all of the palindrome numbers in the given range, but the code is slightly slower than needed. I've tried to improve algorithmic complexity to the best of my ...
7
votes
1answer
546 views

Finding circular primes

I'm coding for a project euler question and every now and then, the question will demand a program that is efficient even when doing brute force. Which I struggle with. Below is a piece of code for ...
7
votes
1answer
733 views

A more optimized way of finding prime factors

I've solved Project Euler #3, but solving the same in Hacker Rank which has time constraint of < 4 seconds doesn't work. I've tried using the square root solution for speeding up but it fails. ...
7
votes
3answers
841 views

Finding the shortest excerpt that contains all search terms

Write a function called answer(document, searchTerms) which returns the shortest snippet of the document, containing all of the given search terms. The search ...
7
votes
3answers
794 views

Rating tennis players in a database, taking days to run

I have this project in data analysis for creating a ranking of tennis players. Currently, it takes more than 6 days to run on my computer. Can you review the code and see where's the problem? ...
7
votes
1answer
3k views

Calculate the running median

I'm trying to solve a challenge where you need to calculate the median every time you add a number. say you have a list of numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ...
7
votes
2answers
2k views

Obtaining a target number only using the operations ×2, ×3, and +1

Problem Introduction You are given a primitive calculator that can perform the following three operations with the current number x: multiply x by 2, multiply x by 3, or add 1 to x. Your goal ...
7
votes
2answers
197 views

Calculating the result of a recurrence function

The recurrence function is defined as follows: \$f(0) = 1\$ \$f(1) = 1\$ \$f(2n) = f(n)\$ \$f(2n+1) = f(n) + f(n-1)\$ I was tasked to calculate the recurrence of a very large number, \$n = ...
7
votes
2answers
130 views

Finding collisions of the first few bits of a SHA-1 hash

My objective is to find a hash collision of my modified hash function. Assuming my modified hash only outputs the first 36 bits of SHA-1. As we know, SHA-1 is a 160-bit hash value, hence, we only need ...
7
votes
1answer
120 views

Google “Wiggle walk”

I am doing Kickstart's problem Wiggle Walk. I have tried to code with matrix, dictionary and nested dictionary but the time exceeds the limit in each submition. Can anyone help me optimize it better ...
7
votes
2answers
124 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 ...
7
votes
2answers
52 views

Creating a list of new areas for patches of conserved land within a watershed

I'm writing code to create a list of new areas for patches of conserved land within a watershed based on the current size distribution of conserved land patches that are in the watershed now. I'm ...
7
votes
2answers
366 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 ...
7
votes
1answer
6k views

Divide all elements in the column of a sparse matrix

Using Python Scipy, I am trying to divide all numbers in all columns of a sparse matrix (400K × 500K, density 0.0005), by the sum of the squares of all numbers in a column. If a column is ...
7
votes
2answers
1k views

Mastermind code taking hours to calculate

I'm trying to implement Knuth's Five-guess Mastermind algorithm in my own version of Mastermind, but when running step 6 it takes my code hours to actually run through everything to get the neccesary ...
7
votes
1answer
1k views

Optimizing string match algorithm

I was trying to solve this problem. I thought of any possible relation between the number of matches among subsequent substrings, but could not find one. So, the probably naive solution that I ...
7
votes
1answer
184 views

Hackerrank: Computer Game (max-flow problem with integer factorization)

I am working on a coding challenge from the Hackerrank site. Given two equal-length arrays of integers, with values from 2 to 109, find the maximum number of times we can remove a pair (Ai, Bj) where ...
7
votes
1answer
547 views

Cheapest flights with at most k stops

This is the Cheapest Flights Within K Stops problem from leetcode.com: There are \$n\$ cities connected by \$m\$ flights. Each fight starts from city \$u\$ and arrives at \$v\$ with a price \$w\$. ...
7
votes
2answers
4k 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 ...
7
votes
1answer
872 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 ...
7
votes
1answer
74 views

Find neighboring PINs on a numeric keypad

I am new to coding and to apply for an internship I need to solve several tasks. This is one of them. I wrote working code, but the program says it takes too long to complete. Help me speed up my code ...
7
votes
2answers
143 views

Heights of Cats

I have an assignment to solve this problem. However, I have exceeded the time limit required, can anyone suggest where to improve my code? Cats like to sit in high places. It is not uncommon to see ...
7
votes
1answer
774 views

Genetic Algorithm for N-queens is very slow

Using Genetic Algorithm to solve N-Queens problem where N=22. My program is functional and is capable of solving N-Queen problems up to around where N=15, but everything after that is absurdly slow. ...
7
votes
1answer
647 views

Similarity research : K-Nearest Neighbour(KNN) using a linear regression to determine the weights

I have a set of houses with categorical and numerical data. Later I will have a new house and my goal will be to find the 20 closest houses. The code is working fine, and the result are not so bad but ...
7
votes
1answer
277 views

Java WordLadder Program

I wrote a program that calculates the minimum number of rungs of a "word ladder" between a starting word and an ending word. The function minlinks() takes in 3 ...
7
votes
3answers
147 views

Splitting a rectangular chocolate bar

I'm having trouble with my school homework. I have a chocolate bar that consists of either black, white or black&white (mixed) squares. I'm supposed to divide it in two groups, one that has only ...
7
votes
1answer
131 views

Speeding up maximum self-similarity test for heavy tail-exponents

I am trying to reproduce results from a research paper using python. I've checked my method and it works on relatively small sample datasets. However, the code does not run for my actual dataset, ...
7
votes
2answers
516 views

Python solution to Code Jam's 'Rounding Error'

The "Rounding Error" problem of Round 1B of Google Code Jam 2018 is as follows: Problem To finally settle the age-old question of which programming language is the best, you are asking a total ...
6
votes
4answers
546 views

Counting multiples of 3 up to a given number

I'm attempting a practice problem in Codeforces that requires you to find the number of multiples of 3 less than or equal to a given number. I have written the following code but it gives a TLE once ...
6
votes
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 ...
6
votes
3answers
851 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 → ...
6
votes
4answers
8k 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 + ...
6
votes
4answers
205 views

Building a toy from Lego toys

I recently attempted a coding problem, but my solution was not well received during the interview (modified slightly to reduce Google-ability). Suppose a child has several Lego blocks, and wants to ...
6
votes
2answers
1k views

Hacker Rank Challenge : Find count of substrings which are special palindrome

I am getting timeout for test cases having very large input. -= Challenge Link =- -= Test case example =- Code is working fine as can be checked on ideone. (Note: Here ...
6
votes
2answers
788 views

Solution to Chef and Squares challenge, timing out in Java but not in C++

I was solving a problem on codechef (online judge and contest organizer). I was writing my code in Java and was getting TLE at last testcase where as I wrote the same code in C++ and it was accepted. ...
6
votes
3answers
552 views

C++ code to read, sort, and write data

I had written code in C++ to create transformation filters to get input, sort (custom_sorting and existing sorting routine) and write the output to stdin and file. When I presented it to my teacher, ...
6
votes
2answers
649 views

Count numbers with atleast one common factor, excluding one (updated)

Below is an updated version of the question I asked prior: Count numbers with atleast one common factor, excluding one I have been trying to teach myself some python 3 by working through problems ...
6
votes
3answers
8k views

E-mail MIME message parser

As part of a larger Java application I'm working on, I have to retrieve emails and parse the data for the emails' content (subject, date, text, attachments, sender). In the method below, I pass a <...
6
votes
2answers
37k views

Printing the n'th prime numbers

This is an online problem. The challenge is to print the kth prime number, for each k given in the input (1 ≤ k ≤ 15000). The first line of input indicates the number of inputs on the subsequent N ...
6
votes
2answers
1k views

Hackerrank Modular Range Queries

Given an array, A=[a0,a1,a2...an], perform queries in the form left right x y. For each query, print the number of elements satisfying the following criteria: left < i < right a[i] ...
6
votes
2answers
8k views

Finding maximum length of continuous string which has same characters

I'm solving this problem from codeforces. I've a solution which has complexity of O(n²). But the time limit exceeds on some of the test cases. The problem statement is: High school student Vasya ...
6
votes
2answers
835 views

Project Euler #48 in C++

This question is similar to Project Euler #48 but constraints are different: $$N < 2000000$$ Just in case the link is unavailable, here is the problem statement: We've to print $$\left( \sum_{...
6
votes
1answer
226 views

Counting primes in a range whose digits are all prime

Codewars Kata: Given integers a and b, count all primes p where a ≤ p < b and the digits of p are all prime. b may be as large as 107. To start with, I hardcoded all valid 1903 primes in \$[1, 10^...
6
votes
4answers
2k views

Sum of even Fibonacci numbers

So, in my first attempt I created a code that generated all fibonacci numbers and appended to a list. This list I then traversed to get the even numbers and added them up. It worked, but took like 5 ...
6
votes
2answers
2k views

Maximum absolute difference of array elements

I am trying to solve a question from array section: You are given an array of N integers, A1, A2 ,…, AN. Return maximum value of f(i, j) for all 1 ≤ i, j ≤ N. f(i, j) is defined as |A[i] -...
6
votes
1answer
99 views

Count swaps needed to order a list of integers

I wrote this code since I'm going through the hackerrank challenges, however, it takes way too long to run. Its function is described on the title, but just to clear it up, we need to count the ...