Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

7
votes
1answer
76 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 ...
8
votes
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 ? ...
1
vote
0answers
31 views

TLE error: Remove element from LinkedList [on hold]

I am working on an easy LinkedList basic operation problem on Remove Linked List Elements - LeetCode Remove Linked List Elements Remove all elements from a linked list of integers that ...
1
vote
2answers
84 views

New Year Chaos JavaScript, needs to be sped up

Similar to this question but this is for Python Original problem with description on hacker rank I am currently trying to iterate through a large number of arrays and count how many times numbers ...
0
votes
1answer
28 views

Time limit exceeded in K-th symbol in Grammar (LeetCode)

I am taking efforts to solve problem K-th Symbol in Grammar - LeetCode K-th Symbol in Grammar On the first row, we write a 0. Now in every subsequent ...
5
votes
1answer
174 views

Finding the upper and lower bound using binary search

I am trying to solve this InterviewBit question https://www.interviewbit.com/problems/search-for-a-range/. My code for this seems to be working properly and it does give me the correct answers on my ...
0
votes
1answer
181 views

Solution to Codejam 2019's Pylons

I'm trying to solve Pylons from the 2019's round 1A. The analysis suggests that a brute force approach should work. However, my Python 3 solution doesn't pass the 2nd test case. Can I make it faster ...
1
vote
1answer
65 views

Merging records from one spreadsheet into another

I have created the code below that compare two spreadsheets . It initially compares the range of value from sheet1 ("Status") to sheet2 ("Interface") . Whenever a range matches with the ranges present ...
3
votes
1answer
62 views

Google Kick Start Practice Round 2019 - Mural

My code exceeds the time limit on the second test set. A suggestion/hint of a better algorithm would be appreciated. Problem Thanh wants to paint a wonderful mural on a wall that is N ...
1
vote
1answer
79 views

Sum of number's quotients when divided by exact powers of 2

Problem I have written code to solve this challenge: Given a formula: GCD(x,y) means the GCD (Greatest Common Divisor) of x and y. For example: if N=2, then: f(2) = 1/GCD(1,...
1
vote
1answer
44 views

Programming Challenge from Kattis: Distinctive Characters

I'm currently working on this problem from Kattis. In short, we're supposed to find the string that has the least similarity to any other string given. Every element of a string is either 0 or 1. ...
1
vote
1answer
33 views

Exceeded time limit to solve super pow

I tried to solve the Super Pow - LeetCode Super Pow Medium Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in ...
2
votes
3answers
101 views

Looking for all numbers where each digit is bigger than the previous digit

This is the "Increasing Number problem", where we are looking for all numbers with \$n\$ digits where each digit is bigger than the digit before it. Or in Mathematical therms where: $$\mathit{...
2
votes
1answer
35 views

Kattis Problem 'Mali'

I wrote a solution to the Mali problem on Kattis: Given inputs a1, a2, a3, …, an and b1, b2, b3, …, bn, determine n pairings (ai, bj) such that each number in the A sequence is used in exactly one ...
4
votes
2answers
95 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 ...
2
votes
1answer
62 views

Transmission Of Messages

I have an assignment to solve this problem. Problem Description Bob the Cat is building a new system, dubbed the Cats Transmission System (CTS). This horribly inefficient system utilises cats to ...
2
votes
1answer
72 views

Finding maximum homework grade ECOO 2018

I'm trying to solve ECOO 2018 round 2(regionals) question 2 to prepare for the upcoming contest. Basically, in the question George has to achieve the maximum possible grade by completing the right ...
0
votes
0answers
39 views

The number having odd divisors between 2 numbers

I am trying to solve a problem from Hackerearth. I'm able to solve sample testcases but somehow when I am submitting the solution it gives time limit exceeded error or wrong output message. Problem ...
3
votes
1answer
321 views

A binary search solution to 3Sum

I tried a binary solution to 3Sum problem in LeetCode: Given an array nums of \$n\$ integers, are there elements \$a\$, \$b\$, \$c\$ in ...
0
votes
0answers
74 views

Hackerrank All Women's Codesprint 2019: Visually Balanced Sections

Link: https://www.hackerrank.com/contests/hackerrank-all-womens-codesprint-2019/challenges/visually-balanced-sections/problem Problem Description A rack has \$n\$ horizontal compartments. The ...
2
votes
0answers
54 views

lowest common ancestor with square root decompostion

This is my implementation of lowest common ancestor problem,I tried to solve it using square root decomposition pre processing time : O(n) query time:O(sqrt(h)) h -> height of tree This ...
3
votes
1answer
44 views

GreetingCard problem with Coordinates

This is a practice problem on Kattis I have exceeded the time limit for the 5th out of 11 test case. Can anyone inspire me what could be done to optimize the code? Problem Description Quido plans ...
4
votes
2answers
79 views

Code to find the sums of building heights

Remains You've recently stumbled upon the remains of a ruined ancient city. Luckily, you've studied enough ancient architecture to know how the buildings were laid out. The city had \$n\$ ...
3
votes
1answer
89 views

Ranking Score System

I have an assignment to solve this problem. There are a total of 12 test case files, 1 of which that I have failed due to exceeding limit on the script. Question Description Bob has somehow ...
2
votes
1answer
41 views

Calculating the potential of a matrix of size (424600, 55)

I have the following working code to calculate the potential of a matrix after normalizing the matrix. But it is too much time consuming. Is there a way to make this code run faster? ...
2
votes
2answers
74 views

Project Euler, Problem 273: finding perfect-square partitions

Problem: Consider equations of the form: \$a^2 + b^2 = N; 0 \leq a \leq b; a, b, N \in \mathbb{N}\$. For \$N=65\$ there are two solutions: \$a=1, b=8\$ and \$a=4, b=7\$. We call \$S(...
7
votes
1answer
283 views

Block storage rewrites

Summary In a block storage system, new data is written in blocks. We are going to represent the flash memory as one sequential array. We have a list of block writes coming in the form of arrays of ...
0
votes
0answers
40 views

Counting events that occur in some time intervals

I have a python code where I need to count the number of ids in a given range of timestamps as : ...
9
votes
2answers
548 views

List elements digit difference sort

I am doing a code challenge of codesignal.com which ask for following things: Given an array of integers, sort its elements by the difference of their largest and smallest digits. In the case of a tie,...
1
vote
2answers
61 views

Determine whether a node is reachable

I am working on this HackerRank problem but running into time constraint issues. Basically, given two arrays, we need to determine if the ith element of the second array is reachable by the ith ...
2
votes
1answer
71 views

Given a string and a word dict, find all possible sentences

I have been trying to figure out how to meet space and time complexity requirements for the following leet code question. I have come up with two versions, one that meets the time requirements and ...
14
votes
5answers
4k views

Hacker Rank: Array left rotation

This code is to solve the Hacker Rank problem array left rotation. A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left. For example, if 2 left ...
1
vote
1answer
39 views

HackerRank Left rotation code using Java 8

I tried to solve this Left Rotation problem on HackerRank. My code works out for most test cases, but for some it doesn't, probably because they have large number of data. So there seems to be some ...
3
votes
2answers
50 views

firstDuplicate of array

This question has been asked for other languages, but I haven't seen it for C++. Most notably here and here. Given an array a that contains only numbers in the range from 1 to a.length, find the ...
6
votes
1answer
211 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^...
1
vote
2answers
62 views

Work out largest prime factor of a number

I am trying to get better at problem solving, so I am going through challenges on Project Euler. Problem: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of ...
5
votes
1answer
45 views

Codewars: N smallest elements in original order

I am consistently timing out on the mentioned kata: Task You will be given an array of random integers and a number n. You have to extract n smallest integers out of it preserving the original ...
1
vote
1answer
77 views

Leetcode 3Sum problem using hashmap in Python

For practice, I solved Leetcode 15. 3Sum question: Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives ...
0
votes
2answers
74 views

Select nth smallest distinct integer from inputs

Kata in question Given a list of integers, return the nth smallest integer in the list. Only distinct elements should be considered when calculating the answer. n will always be positive (n > 0) ...
0
votes
1answer
126 views

“Food Ordering” program exceeds time limit in test 9

So, I've recently started working on CodeForces to practice from older contests (and maybe even start participating in current ones), but there is one problem that I can't seem to make an efficient ...
3
votes
1answer
229 views

Word break problem with recursion and memorization [closed]

For practice, I solved Leetcode 139. worbreak question: Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-...
2
votes
2answers
208 views

Mr. Muffin's ball-passing game

I have an assignment to solve this problem. There are a total of 18 test case files, 5 of which I have failed due to exceeding some time limit on the script and I am unable to verify if there are any ...
3
votes
2answers
55 views

Find next number whose sum of digits is N

This program takes two numbers M(<10k) and N (<100) and finds the smallest number greater than M whose sum of digits = N. Additionally, as per the question's requirements, it prints the number ...
9
votes
4answers
1k views

Compute binary XOR of all integers in a range, mod 2

here I am taking 3 inputs. 1st input will take test cases and the other two input will take starting and ending range. the program is running fine as expected but the limit of this question for ...
0
votes
0answers
133 views

Time limit exceeded on HackerRank “Username Disparity”

Here is the hackerrank question: For two strings A and B, we define the similarity of the strings to be the length of the longest prefix common to both strings. For example, the similarity of ...
2
votes
0answers
30 views

Solving the wooden snake chain puzzle

My code tries to solve the wooden snake chain puzzle: I do so by generating all possible link configurations which are then tested. I was hoping that laziness would take care of the very many ...
3
votes
0answers
116 views

Given an input string and a pattern, implement wildcard pattern matching

I have been trying to solve "Wildcard Matching" on leetcode. I know there are more "manual" ways to solve this without using the RE library. But I would like to know if passing the time limit is ...
2
votes
2answers
209 views

The next prime number in Python

I have written a solution to a DM::OJ exercise to find the next Prime Number after a given number (input). I'm seeking to optimize it so that it will run for any number up to ...
2
votes
1answer
90 views

Langton's Ant simulation

I created a simple program to simulate the Ant's path. The Ant moves north, west, east and south. Each time the Anth moves to an "uncharted" location, I build a Square and give it a color. Each time ...
1
vote
3answers
131 views

Kattis prime reduction challenge

I am solving the Prime Reduction challenge on Kattis. Consider the following process, which we’ll call prime reduction. Given an input x: if x is prime, print x and stop factor x into its ...