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

913 questions
Filter by
Sorted by
Tagged with
12 views

### Astar algorithm exceeds time limit

I have code that reduces the array until all remaining elements are equal. It takes a first line which contains an integer n, the number of elements in the array, and a second line which contains ...
61 views

### Graph: Depth First Search (N citizens with pairs of friends)

I have solved problem 10608 on UVA Online Judge using Python 3.5.1. My solution works, but it takes too long to run when the online judge evaluates it. Problem There is a town with N citizens. It is ...
89 views

### 01 Matrix is too slow when solved using DFS

I tried solving Leetcode 01 Matrix problem. It is running too slow when solved using DFS approach. Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance ...
44 views

### Twitter contact scraper with Tweepy & Django

I created a project for academic purposes that works on Twitter data. It should: Get all friends and follows of the user Store them in MongoDB Display them in a table I'm using django + plain HTML, ...
73 views

### Efficiently calculate the value of an arithmetic sequence

I need to calculate the value for the following function: $$f(n) = -1 + 2 - 3 + ... + -1^n$$ The time limit to calculate a given value of $n$ is less than 1 second. My approach does not meet that ...
65 views

### HackerRank - Binary tree to compute the Nth power of natural numbers that sum up to X

I'm trying to solve this challenge on HackerRank. In short, given x and n, I have to determine how many ways I can pick numbers ...
34 views

### Count number of square integers in a given range of ints (faster function?)

The function below passes most test cases: ...
875 views

### Better performance in solution for the Dole Out Cadbury challenge

Dole Out Cadbury Problem Description You are a teacher in reputed school. During Celebration Day you were assigned a task to distribute Cadbury such that maximum children get the chocolate. You have ...
144 views

### Cross-Entropy Minimization - Extreme Code Performance

I'm working on a multivariate cross-entropy minimization model (for more details about it, see this paper, pp. 32-33). It's purpose is to adjust a prior multivariate distribution (in this case, a ...
126 views

### Prime factorisation in java

I came across this question in a coding contest (Java-restricted) and I got TLE. I am unable to provide the link of the question as the contest is closed now. Can I know how can I optimise this? Four ...
67 views

### Count unique subsequences

I came across this question in a coding competition (Java-restricted) and I got a time-length-exceeded. I am unable to provide a link as the contest is closed now. Can I know how can I optimise this? ...
95 views

### Programming challenge - report fraudulent banking activity times out

How can I optimise this code there is time out error Program Challenge: HackerLand National Bank has a simple policy for warning clients about possible fraudulent account activity. If the amount ...
353 views

### Finding the first pair of prime numbers that have some specified difference

I tried printing the first pair of prime number in between a list of numbers with a particular difference. But my run time is still too high (for large range of numbers). I want to reduce the run time ...
43 views

### Finding the lowest perfect square that can be added to a number to make another perfect square

The code below returns the smallest perfect square that can be added to a number n to result in a perfect square. It works perfectly fine but I need it to run ...
649 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 ...
70 views

### “Jedi's riddle” challenge

The challenge can be found here. In short we have a recursive function, and we need to find when $z_i$ is equal to one.  \begin{align} z_1 &= 1\\ z_i &= z_{i-1} * a\ \%\ n\\ \...
171 views

### Minimize sum of array by applying specific operation on x carefully chosen elements

This code works, and solves the problem, but doesn't satisfy some time requirements, especially when the 'nums' array can have a length of over 1000. How can this be optimized? Just reference: This ...
47 views

### Time limit exceeds in this simple program [closed]

Link to problem: https://www.codechef.com/JUNE20B/problems/EVENM Iam trying to solve a simple problem challenge, but getting TLE. I had a different approach earlier, so tried this, again with no ...
59 views

### Recursive brute-force approach to maximum points you can obtain from cards

I came across this question on Leetcode. The question description is as follows: There are several cards arranged in a row, and each card has an associated number of points. The points are given in ...
38 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 ...
138 views

### Count number of previous elements greater than its element at present index

Suppose I have a vector containing n elements. I want to find out the number of previous elements greater than its element at present index i. I want to find ...
33 views

### How do I quickly look for parents containing their own code as child?

I have tree-like data that is constructed of Parent codes that contain Child codes which may act as parents themselves, depending on if they're marked as "SA". This data is present in an Excel sheet ...
70 views

### How to work in linear time with DFS

I tried a Hackerrank problem and it gives me a successful message, it works fine for Hackerrank criteria. The member states of the UN are planning to send 2 people to the moon. They want them to be ...
123 views

### Leetcode three sum in Javascript

I was doing 3sum question 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 sum ...
122 views

### How can I optimize my code that uses nested for loops?

I am working to solve a problem where I need to determine if a Point lies on a line connecting two other Points. For example, ...
30 views

### HackerRank's Challenging Palindromes solution times out for certain tests

I am trying to solve the Challenging Palindromes problem from HackerRank. The code that I have got so far fails only for large inputs due to timeout, every other test it passes successfully. The ...
89 views

### Binary Search implementation: HackerRank - Climbing the Leaderboard

My code passes 11 out of 12 test cases. I was wondering where I can improve my code. NOTE: This code needs performance improvement, as it is working for most of the cases. To mu knowledge it would ...
92 views

### Calculate your total happiness, which depends on the integers in the given sets [closed]

I am a beginner in programming. And I am learning python as my first language. I went on to do some challenges on HackerRank and stumbled upon this problem. The problem goes like this: There is an ...
163 views

### Maximal Char Requests in a String

I have been trying to deal with this problem set, and I have figured out how to solve it as well. But there is one problem that is ...
88 views

### Which products should be purchased with the given amount of money: Hackerrank?

I am writing code for the challenge given here . Below the text: Each time Sunny and Johnny take a trip to the Ice Cream Parlor, they pool their money to buy ice cream. On any given day, the ...
824 views

### What should I do to reduce run time of my code?

This is a URI online judge problem (problem no: 1973). Link : https://www.urionlinejudge.com.br/judge/en/problems/view/1973 After buying many adjacent farms at the west region of Santa Catarina, ...
104 views

### Checking which polygon a set of coordinates lie inside

I have a massive set of latitude, longitude pairs. I also have a list of GeoJson formatted locations. I want to find out which location each pair of coordinates lies in. I have some code to do this, ...
33 views

### Why is my function timing out when run with a larger data set?

I am working on a Code Kata ...
40 views

### Get average post score of TOP 10 tags on StackOverflow using Stack Exchange Data Explorer

I want to use the Stack exchange data explorer in order to display some basic information about the top tags. The query should print the number of questions and answers, the average number of answers ...
40 views

### Wildcard Matching with DFS and FSM

I'm trying to solve Wildcard Matching on Leetcode. I found a great solution using the Finite-state machine. The author builds an FSM and searches with BFS. I rewrite the solution with the same idea (...
167 views

### Finding median of sub arrays taking too much time

I'm trying to solve a programming challenge where I have to find the median of given subarrays of an array. I have used std::vector's ...
194 views

### The Great Julya Calendar

Challenge An integer is provided as an input. In each step, we have to subtract a number from the input which already exists as a digit of the input. In this way, we have to make the input ...
94 views

### Best Travel Kata C++

https://www.codewars.com/kata/best-travel Briefly, the problem states that we have to create a function choose_best_sum(int t, int k,std::vector<int> xs) ...
235 views

### Codewars: Prime Streaming (PG-13)

I'm timing out on Codewars' Prime Streaming kata. Below is my code: ...
100 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 ...
592 views

### Dynamic Array Problem (Hacker rank)

I am trying to solve the Dynamic Array problem on HackerRank: Create a list, seqList, of N empty sequences, where each sequence is indexed from 0 to N-1. The elements within each of the N ...
80 views

### Designing a stack such that getMin() is both constant time and constant space

I'm attempting the SpecialStack problem on GeeksforGeeks. Design a data-structure SpecialStack (using the STL of stack) that supports all the stack operations like push(), pop(), isEmpty(), ...
49 views

### Flash Memory block write counting & thresholding

I am trying to solve this question: 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 ...
58 views

### Geting maximum prime factor for a given number [closed]

I'm trying to solve a problem from Project Euler: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? and here is my attempt: ...
34 views

### python segment tree - count of smaller numbers after self

I am trying to solve this question https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nums and you have to return a new counts array. The counts ...
83 views

### Find all pairs of numbers such that the number of binary 1's in their OR is maximum

So, I tried to solve this question and my code did not execute within time-limits. https://www.hackerrank.com/challenges/acm-icpc-team Here's my code, ...
125 views

### Python Fenwick Tree

I am trying to solve this question: https://open.kattis.com/problems/fenwick A Fenwick Tree (also known as a Binary Indexed Tree) is a data structure on an array which enables fast (𝑂(log𝑛)) ...
119 views

### Find positive integer with a minimum number of binary 1's such that its bitwise AND with all array elements is not 0

Task You have an array of positive integers. Your task is to find an integer x such that the following are true: Bitwise AND between ...