# 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.

959 questions
Filter by
Sorted by
Tagged with
2answers
78 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 ...
1answer
57 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 ...
3answers
35k views

### Minimum number of swaps required to sort the array in ascending order

Here is my problem statement. An excerpt: You are given an unordered array consisting of consecutive integers ∈ [1, 2, 3, ..., n] without any duplicates. You are ...
1answer
118 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 ...
5answers
3k views

### Enhanced Hashmap - Add a number to all keys/values

I had the below problem in a coding test and I got 28/30 tests passes and 2 failed due to a time-out. Problem You have created a programming language and now you have decided to add ...
1answer
41 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 ...
2answers
3k views

### Evaluating a completely parenthesized arithmetic expression

I tried to solve the following problem in a programming challenge, but the verdict was time limit exceeded. Completely parenthesized expression Write a program that reads a completely ...
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 ...
3answers
9k views

### Calculating the sum of array elements to the left and right

I am using C++ to code the following logic: An Array is given we have to traverse the array such that sum of array elements to its left must be equal to sum of elements to its right. BTW this is ...
1answer
51 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 ...
2answers
59 views

...
2answers
3k views

### Solution to King Kohima problem in Python

King Kohima problem: King Kohima has reserved a new exclusive street for his executive class employees where they can build their homes. He has assigned you to plan that street. You have to ...
2answers
134 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 ...
1answer
148 views

### Given an integer array A and a range, find a subarray within the range with a minimum sum

Problem HackerRank Range query: You are given an array A of n integers. You have to perform ...
2answers
71 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. ...
1answer
56 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 ...
1answer
49 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 ...
2answers
106 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 ...
1answer
90 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 ...
1answer
51 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 ...
5answers
84 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 ...
2answers
73 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 ...
4answers
471 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 ...
1answer
53 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 ...
1answer
50 views

### Extracting emails from log files

I have huge number of text files, each several hundred MB in size. Unfortunately, they are not all fully standardized in any one format. Plus there is a lot of legacy in here, and a lot of junk and ...
2answers
120 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 ...
2answers
64 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/...
1answer
131 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: ...
1answer
1k 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 ...
1answer
895 views

### Time complexity of Max counters

As per the instructions are given in MaxCounters-Codility, You are given N counters, initially set to 0, and you have two possible operations on them: <...
2answers
129 views

...
5answers
664 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 ...
1answer
135 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 ...
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 ...
0answers
80 views

### HMRO - Help the Military Recruitment Office

I am solving this problem on SPOJ but I am getting Time Limit Exceeded Error. https://www.spoj.com/problems/HMRO/ At the end of year 2004, the regional agencies of the Polish Military Recruitment ...
3answers
3k views

### Hackerrank challenge - Apple and Orange

The problem I am trying to solve is this. Apples fall at various offsets relative to an apple tree at position a, and oranges fall at various given offsets relative to an orange tree at position b. ...
2answers
3k views

### HackerRank Lower Bound-STL

This is still my first week of learning C++, I solved the lower bound STL challenge in HackerRank Lower Bound STL but I'm having time exceeded issue for long input. I'm open to constructive criticism, ...
1answer
79 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 ...
2answers
1k 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 ...
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 ...
2answers
95 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 ...
1answer
60 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 ...
1answer
356 views

### Minimum window substring - LeetCode challenge

Below is my code for the “Minimum Window Substring” LeetCode problem in Swift: Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(...
2answers
71 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 ...
1answer
204 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 ...
1answer
827 views

### Finding the distance between the two closest points in a 2-D plane

Here is my code: ...
2answers
148 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 ...
1answer
89 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 ...
2answers
179 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 ...
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_{...