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
5
votes
3answers
138 views

Outputting prime numbers in given ranges

I coded an algorithm which shows prime numbers in the ranges provided by the user. The first line states the number of test cases. Example: INPUT 2 1 10 1 5 ...
5
votes
4answers
4k views

Bit shifting in Python

I am trying to "decode" a file which has every byte shifted its bits. I have successfully managed to decode this by reading the file, and then byte by byte doing the following (...
5
votes
2answers
390 views

Project Euler problem 530 - GCD function is inefficient

Here is a description of Project Euler problem 530: Every divisor \$d\$ of a number \$n\$ has a complementary divisor \$n/d\$. Let \$f(n)\$ be the sum of the greatest common divisor of \$d\$ ...
5
votes
2answers
75 views

Creating nodes and edges

I'm looking for a way to optimize this piece of code in Python. all_users holds all the Twitter users and friends_ids is an ...
5
votes
2answers
2k views

Prime generator program SPOJ time limit exceed

Problem Statement Input The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= ...
5
votes
1answer
295 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 ...
5
votes
1answer
214 views

CodeForces: Creating the Contest

I wrote a solution to this question. The task is to take an input of n increasing positive integers (1 ≤ n ≤ 2∙105), each entry as large as 109, and find the length of the longest subsequence where ...
5
votes
1answer
6k views

Shortest Path For Google Foobar (Prepare The Bunnies Escape)

I have been working on Google Foobar since a few days ago. I am currently in the third level but is stuck in the second challenge of "Prepare the Bunnies' Escape." I have checked this post but it did ...
5
votes
2answers
1k views

Predict the best day to buy and sell the shares

I was trying out my hand at solving problems and came across this HackerRank problem. I have written a solution which passes for a few test cases but times out for most others (time limit - 3 seconds)....
5
votes
3answers
130 views

Compose valid subtractions using all digits from a set

I have created a brainteaser, which I called the "impossible problem" (see this video I made). The aim is to create a valid 3-digit subtraction which uses every digit from 1 to 9 once. Here is an ...
5
votes
3answers
1k views

Finding nearest power of 2

I have the following simple program which calculates and returns the nearest power of 2 of a given number. Because I use increments and decrements to find the closest power, this takes a very long ...
5
votes
3answers
3k views

Performing several integer division operations on an array

Program: Given an array A of size N. Given Q operations, each operation contains an integer D. In each operation you have to divide all the elements of the array by D. For example, for ...
5
votes
6answers
5k views

Counting pairs of relatively prime numbers

Problem from Hacker Earth: Inverted GCD: Given an array a of \$N\$ numbers , you have to find the number of pair of indices \$i\$ and \$j\$ that satisfy the following relation: \$i <...
5
votes
3answers
721 views

Phone List challenge

To learn Haskell, I've been going at this challenge, which is to check a list of phone numbers to ensure that no number is a prefix of another. However, my code is way too slow. I am really new to ...
5
votes
2answers
2k views

Reverse Game HackerRank Ruby Solution

Problem Statement Akash and Akhil are playing a game. They have N balls numbered from 0 to N−1. Akhil asks Akash to reverse the position of the balls, i.e., to change the order from ...
5
votes
1answer
85 views

Finding sum of divisors of numbers between the range (1 to 1e16)

The exact link to the question : https://www.spoj.com/problems/DIVSUM2/ Input: An integer stating the number of test cases (equal to 500), and that many lines follow, each containing one integer ...
5
votes
3answers
577 views

Python generator for “barely obtuse” triangles (Project Euler 224)

I am trying to solve Project Euler Problem 224: Let us call an integer sided triangle with sides a ≤ b ≤ c barely obtuse if the sides satisfy a2 + b2 = c2 - 1. How many barely obtuse triangles ...
5
votes
1answer
90 views

Web page downloader that supports redirects

I wrote a class that downloads the HTML of a page from the internet. This class also manages redirects. This works well but I'm not very satisfied about time management for slower servers. In fact, if ...
5
votes
2answers
852 views

High execution time of LCS length program in Python2

I was trying to solve the Longest Common Subsequence problem on a HackerRank exercise. It's quite simple and straightforward, just print out the length of the LCS. I submitted this code: ...
5
votes
2answers
96 views

Birthday paradox simulation

I'm writing a simulator which checks how many balls we need to occupy every box or how many balls we need for any box has at least 2 balls (birthday paradox). I wrote the script in python and it is so ...
5
votes
2answers
307 views

Hackerrank.com - Sherlock and Anagrams

Summary: Given a string find the number of anagramic pairs of substrings of it. e.g 'abba' pairs are [a,a],[b,b],[ab,ba],[abb,bba] so we have 4 pairs. I'm wondering if anyone can help me improve my ...
5
votes
3answers
156 views

Finding black cluster size distribution for square grid at different probabilities of black elements (averaged over a certain number of iterations)

Context: I had asked a related question on Stack Overflow. On receiving several helpful hints from the commentators there, I could successfully implement averaging of the size distribution over a ...
5
votes
3answers
236 views

Calculating total maintenance costs of a car

Introduction I drive a white 2CV from home to work, and intend to keep that way until retirement. Once retired, I will sell the 2CV to buy a yacht and sail away. My home is far, far away from ...
5
votes
2answers
315 views

HackerRank - Non-Divisible Subset

In this problem we are asked: Given a set, S, of n distinct integers, print the size of a maximal subset, S', of where the sum of any two numbers in it is not evenly divisible by k. Code My ...
5
votes
2answers
2k views

HackerRank Similar String

Problem Statement: https://www.hackerrank.com/challenges/similar-strings I got the logic correct. However it times out for testcase 8 onwards I would like some suggestions on optimizing this code <...
5
votes
2answers
1k views

Phone number to words based on the dictionary

This is the problem: Given a 10 digit phone number, you must return all possible words or combination of words from the provided dictionary, that can be mapped back as a whole to the number. ...
5
votes
2answers
347 views

Practical number algorithm

I am trying to write a program to find the practical numbers, from an input from \$1\$ to \$n\$. Practical numbers My code is running correctly but it is extremely slow when calculating numbers ...
5
votes
1answer
2k views

Evaluating a series of Legendre polynomials

The following function represents the electrostatic potential, in spherical coordinates, due to a ring of charge \$q=1\$ and radius \$R=1\$, placed in the plane \$x\$-\$y\$: $$\phi(r,\theta) = \sum_{...
5
votes
1answer
598 views

Number of divisors of a factorial

I was solving the DIVFACT problem from Sphere Online Judge: Given a number, find the total number of divisors of the factorial of the number. Since the answer can be very large, print the answer ...
5
votes
1answer
132 views

Calculating diagonal difference

I was trying to solve a problem concerning the difference of the diagonal sums of a matrix (using C99): ...
5
votes
1answer
229 views

Project Euler, Challenge #12 in Swift

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, ...
5
votes
1answer
93 views

“Laser treatment” SPOJ challenge

I am trying to solve this problem on SPOJ Brazil. I'm trying to approach the problem with a series of logical conditions and my code looks like this and it works perfectly. ...
5
votes
2answers
4k views

Optimizing O(m n) solution for longest common subsequence challenge

Given two strings string X of length x1 and string Y of length ...
5
votes
2answers
6k views

Finding the next palindrome of a number string

Here is the problem: A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive ...
5
votes
1answer
136 views

Counting out game with std::list

I recently tried to solve the Josephus Problem on Sphere Online to help answer this question but am getting TLE for any solution I come up with. The exact parameters of the problem are: This is ...
5
votes
1answer
78 views

Binary searching the turning point of a function

The problem You are given a list of \$ n \$ positive integers. Your task is to split the list into \$ k \$ subarrays so that the largest subarray sum is minimized. The input On the first line you ...
5
votes
1answer
470 views

Goldbach's Conjecture

I am doing an exercise on finding the nearest pair of prime numbers p₁ and p₂ such that: p₁ ≤ p₂ p₁ + p₂ = n (for 4 ≤ n ≤ 10⁷) p₁ and p₂ are primes p₂ - p₁ is minimal I think it should work ...
5
votes
2answers
118 views

Estimating the number of tanks based on a sample of serial numbers

I created code that was supposed to determine how well my system of guessing is. The scenario is that there are n amount of tanks. Each tank is uniquely numbered 1 to n. Seven of these tanks are ...
5
votes
2answers
677 views

Alphabetizing the layers in Adobe Illustrator

I have a script that alphabetizes the layers in Adobe Illustrator. It does so by sorting the layers in an array (using string names) and then using that to change the item order position (zOrder) of ...
5
votes
1answer
920 views

MaxCounters solution

I am doing this Codility problem You are given N counters, initially set to 0, and you have two possible operations on them: increase(X) − counter X is increased by 1, max counter − all ...
5
votes
2answers
373 views

Number of ways to make change for an amount

Task Write a program that, given the amount to make change for and a list of coins prints out how many different ways you can make change from the coins to STDOUT. My approach The number to make ...
5
votes
1answer
99 views

Optimal way to annihilate a list by removing items from the ends

I'm stuck with a contest problem. Description: What is the minimum number of moves to annihilate an int array where the possible moves are: Remove both the first and last elements if they are equal ...
5
votes
1answer
6k views

Time Limit Exceeds [HS11DIVS] - How to make it faster?

Here is the question. From SPOJ [HS11DIVS]. Now , the problem is that my code works perfectly for small numbers. But when it comes to Large numbers , the time limit exceeds. I am not getting any hint ...
5
votes
1answer
92 views

Timeout Error in Fraudulent Activity Notification HackerRank

I am solving this problem: Fraudulent Activity Notifications on HackerRank. I am done with my code and is working, but it is inefficient as well for very large inputs. I don't know but after all ...
5
votes
2answers
282 views

Hackerrank “Almost Equal” solution

I've spent the best part of a day on this question. It is marked as Expert level. There are about fifteen submission test cases and my solution manages to satisfy the first four. However, from there ...
5
votes
2answers
704 views

Longest subarray with a zero sum

I'm working through problems on this site. The problem I'm currently doing is: Given an array having both positive and negative integers, your task is to complete the function ...
5
votes
1answer
982 views

Pole (Hackerrank)

Problem Description Kevin was thinking about telephone poles and came up with an idea for a fun programming challenge. There are n telephone poles ascending a mountain and each pole has a weight ...
5
votes
1answer
478 views

Hackerrank “Bricks Game”

Problem URL This is the solution I came up with the minimax problem stated above. I did a top-down recursive approach with memoization, and I think this should be \$O(n)\$, since we are filling out a ...
5
votes
2answers
712 views

Task Parallel Library and Mailkit

I have a use case where I need to process about 80,000 emails per day. The client has an existing process that takes about 1/sec per email and is running out of time before the next batch comes in. ...
5
votes
1answer
190 views

How to lose plants and aggravate people

Courtesy of a few posts around here I've discovered HackerRank. Poisonous Plants is one of their challenges. Of course, skip the following if you'd like to try it yourself. Challenge: There are \$...