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
8
votes
3answers
1k views

Find the number of trailing zeros in factorial

I am trying to find the "Number of trailing zeros of N!" (N factorial). In the python code I am trying to find the factorial first and then the number of trailing zeros. However, I am ...
1
vote
1answer
41 views

Maximum Profit in Job Scheduling - Performance Issue

I implemented the Maximum Profit in Job Scheduling algorithm in JavaScript, but I'm having performance issue. The problem: We have n jobs, where every job is scheduled to be done from startTime[i] to ...
3
votes
1answer
29 views

Attempting to solve minimum window substring problem using Python 3.8

Hello programming gurus/reviewers, I attempted to solve the Minimum Window Substring (Leetcode 76) problem using Python 3. My solution involved using Counters and backtracking. On submission, the ...
-1
votes
0answers
28 views

Prime pair sets | Project Euler #60 | Need code optimization

I am on problem 60 of Project Euler and my code is too slow to find the required 5 numbers. I can find 4 numbers easily under a second but finding 5 is taking very long. Can you help me identify what ...
0
votes
3answers
124 views

Calculate sum of a substring

I try to solve some old Codeforces questions. The problem is; Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up q ...
1
vote
1answer
59 views

Non equi self join with basic python libraries

I'm trying to do a non equi self join with basic python libraries on a table that has 1.7 millions rows and 4 variables. the data look like this: ...
1
vote
1answer
63 views

Google Foobar challenge optimization

I am trying to solve Google Foobar challenge prepare-the-bunnies-escape using my own approach, instead of BFS. I attempt to solve two ends of the maze and try to ...
2
votes
3answers
153 views

Finding n-th smallest Palindrome number (where 1<=n<=5000 and the palindrome number must have odd number of digits)

I am working on this code from 3 days from a website called Codechef, the code compiles, executes and even give correct results but my code is taking 1.01 sec, but the time limit for question is 1sec,...
2
votes
1answer
235 views

Codechef: The Chefora Spell

This is the question currently I am trying to solve of codechef and I am able to get the given test cases result but I am getting Time Limit Exceeded when I am trying to submit. Please let me know ...
4
votes
1answer
362 views

Find next smallest number that's representable as a sum of distinct powers of 3

I'm solving this challenge: Given a positive integer number, you have to find the smallest good number greater than or equal to the given number. The positive integer is called good if it can be ...
0
votes
1answer
61 views

Cheapest flights within k stops algorithm in JavaScript

The problem: There are n cities connected by some number of flights. You are given an array flights where flights[i] = [fromi, toi, pricei] indicates that there is ...
2
votes
2answers
80 views

Finding pair of numbers that minimize a function

This is a problem of a past bioinformatic contest (requires an account). My solution works but is too slow for some test cases. Input format The first line of the input contains one integer \$T\$, \$(...
1
vote
2answers
83 views

Compute members of the infinite series generated by 2n+1 and 3n+1

Consider the infinite sequence u, where u is defined as follows: The number u(0) = 1 is the ...
2
votes
2answers
89 views

Keeping only every nth element in an array in JavaScript

I have two arrays, arr_of_frequencies and float_array, of the same length — around 500-800k values each. I have to get that down ...
2
votes
2answers
108 views

return the sum of the squares that is equal to n²

I'm struggling to solve this Kata. My code works but I can't pass because it's timing out. I have googled it and even tried other people's code but still doesn't pass. Any suggestion on how can I make ...
3
votes
1answer
71 views

Java i/o speed less than python i/o while printing an array

Motivation: I was solving an array based i/o problem and encountered Time Limit Errors, it was later found that the code for java ran roughly 10x slower than the ...
-2
votes
1answer
120 views

HackerRank frequency queries

need some help or advice, HackerRank's frequency-queries challenge, the challenge of this Hackerrank problem is to check if the frequencies of number in your data structure matches the queries. small ...
0
votes
1answer
42 views

Return the prime numbers that are dividers of a some numbers in a list

What I need to do is to return the prime numbers that are dividers of a some numbers in a list, so if I = [12, 15] # result = [[2, 12], [3, 27], [5, 15]] and this ...
-3
votes
3answers
104 views

Is there any way to use a single for Loop for this code? [closed]

I want to use only one for Loop for this code. Is this possible? If not then how can I optimise my code further? It works well for the constraint 1 ≤ T ≤ 10, 2 ≤ N ≤ 10^3, 1 ≤ M ≤ 10^5, but I am ...
4
votes
1answer
54 views

Calculate probability that knight does not attempt to leave chessboard after at most k random moves (LeetCode #688)

I'm learning competitive programming and came across this question on LeetCode : 688. Knight Probability in Chessboard On an n x n chessboard, a knight starts at the cell (row, column) and attempts ...
0
votes
2answers
136 views

Count the number of arithmetic progressions within a sequence

I have some problems with code for my classes. Even though it works correctly, I run out of time for half of the examples. Here's the task (I really did my best trying to translate it): You have a ...
1
vote
1answer
50 views

Sort and Delete duplicate image files

I'm trying to make a console application to sort and delete duplicate image files. I have a folder with about 20000 images which take up roughly 70GB of space. I wrote the following script and it ...
1
vote
2answers
72 views

Inserting data into MySQL

My code currently doesn't insert data into MySQL fast enough. However I don't know how to measure the current speed. ...
2
votes
2answers
127 views

Can I further optimize this solution for HackerRank's “Making Candies”?

My C++ solution for HackerRank's "Making Candies" problem, reproduced below, is as optimized as I can make it and appears similar to what others say are optimal solutions. However, six of ...
2
votes
1answer
54 views

Leetcode 3 sum code optimisation

I was working on 3sum problem on leetcode 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 the ...
0
votes
5answers
91 views

Determine whether a magazine contains the words needed for a ransom note

This is a website Question on Hackrrank called Hash Tables: Ransom Note: Given the words in the magazine and the words in the ransom note, print "Yes" if we can replicate the ransom note ...
-2
votes
2answers
75 views

Hackerrank: Absolute Permutation - How do I make this code more efficient

The problem is basically this: Return the permutation of the set of the first n natural numbers which satisfies the following property: |pos(i) - i| = k ∀ i ∈ (1,n) where pos(i) is the ith number in ...
1
vote
4answers
552 views

“Digit Queries” CSES Solution giving TLE

I really need a more accurate way to size inf_string. This code works but gets exponentially more inefficient the bigger max(queries) is, and therefore just can't hold up to the gargantuan numbers ...
2
votes
1answer
57 views

Performance issue in python- network creation based on the Euclidean distance with nested for loops is too slow

I want to create a network in which the links are formed based on a similarity metric defined as the Euclidean distance between the nodes. The distance is calculated using socio-demographic features ...
3
votes
1answer
99 views

Optimizing Dijkstra on grid Python (Microsoft Interview)

Given a square grid of size N, each cell of which contains integer cost which represents a cost to traverse through that cell, we need to find a path from top left cell to bottom right cell by which ...
3
votes
2answers
132 views

How to speed up the code for LeetCode “Container with most water” task?

I'm trying to solve the LeetCode question where you need to find out the area of the container with the most water. I have created the solution that seems to work in my testing but it fails for being ...
2
votes
2answers
67 views

CSES - Number Spiral - Java TLE

My java code for CSES Introductory problem Number Spiral gives TLE for large inputs, like Input : 100000 170550340 943050741 121998376 943430501 689913499 770079066 586095107 933655238 … (First line/...
-1
votes
1answer
146 views

Generating permutations fast

I run into a performance bottleneck in a program. I have to generate all permutations for a given sequence with size up to n=11. My code: ...
4
votes
2answers
129 views
2
votes
1answer
138 views

Counting binary substrings

The exercise i am working on requires me to read a binary sequence stored in a text file. The objective is to find the occurrences of all subsequences of length between a and b (their values are given ...
11
votes
5answers
684 views

Finding reversed word pairs

I wrote this code to find opposites of words (i.e., words backwards). Input is a list of words. If this list contains a word + its opposite, then it should form a pair. The output should be a list of ...
1
vote
1answer
88 views

calculate tree height (arbitrary tree) - python

Given an array as input, we have to calculate the tree height. For example, input [4, -1, 4, 1, 1] means that there are 5 nodes with numbers from 0 to 4, node 0 is a child of node 4, node 1 is the ...
2
votes
1answer
57 views

Calculating edges of a social network

I am working on an open Chess data-set (~15500 rows after cleaning), and I create nodes and edges. But the way I create the edges takes a bit of time. A sample of my nodes tibble: player 1 bougris ...
3
votes
2answers
2k views

Determine the longest segment of integers within an inclusive range that doesn't contain a bad number using Python

I am new to python. Got a question in Hackerrank. Given an Array of bad numbers and a range of integers, how can I determine the longest segment of integers within that inclusive range that doesn't ...
1
vote
1answer
62 views

The Next Palindrome - CodeChef

This is codechef beginner problem. How I can make it efficient, It is taking 2.09 sec while time limit is 2.0764 secs. Problem is: For a given positive integer K of not more than 1000000 digits, write ...
0
votes
2answers
73 views

Find the smallest triangular number with more than 500 factors

I wrote a program about triangular numbers, which are derived from the sum of natural numbers one after the other. For example, the third triangular number is equal to: 1 + 2 + 3 = 6. I want to find ...
2
votes
1answer
63 views

Count the different positions reached in a cycle of permutations

I have come across a problem on cyclic permutations. It is about swapping numbers around. It starts out with input N < 100,000 which makes the first N natural numbers as orders: 1 2 3 ... N-1 N ...
-3
votes
1answer
217 views

HackerRank | Roads and Libraries - Code Optimization

Major Edit: I wish people threw in a comment regarding what they are expecting instead of downvoting this question. I am quite appreciative of constructive feedback. I know it is only 3 people out of ...
3
votes
2answers
98 views

Finding minimum steps required to traverse from source to terminal

Hello everyone this is my first post here so please do critique my post if there's anything. I have made a program intended to pass a programming challenge on open.kattis.com, here. It should do as ...
2
votes
1answer
105 views

Geeks for Geeks: Trapping Rain Water Problem - time limit exceeded

Problem Statement: Given an array arr[] of N non-negative integers representing the height of blocks. If width of each block is 1, compute how much water can be trapped between the blocks during the ...
2
votes
2answers
164 views

Determine whether a number and its square are both the sum of two squares

Code gets timed out and needs to be simplified. To ensure that n = sum of squares , To determine if n^2 = sum of squares , I need to input 4 integers : 5881, 2048, 2670, 3482 ...
3
votes
3answers
114 views

Summation of large inputs

Assume we are given \$ n \$ natural numbers, \$a_{1}, a_{2}, \dots, a_{n} \$ and we are supposed to calculate the following summation: \$ \sum_{i = 1}^{n}\sum_{j = 1}^{n} \left\lfloor \dfrac{a_{i}}{a_{...
1
vote
2answers
180 views

Determine whether bitwise-OR of sub-array is odd

Problem: Given an array A of N integers, you will be asked Q queries. Each query consists of two integers L and R. For each query, find whether bitwise OR of all the array elements between indices L ...
2
votes
5answers
103 views

Revised: Counting subnumbers divisible by a given prime

Assume we are given a number and a prime p. The goal is to count all subnumbers of number ...

1
2 3 4 5
20