Time limit exceeded(TLE) Error in Python code - TagMerge
2Time limit exceeded(TLE) Error in Python codeTime limit exceeded(TLE) Error in Python code

Time limit exceeded(TLE) Error in Python code

Asked 1 years ago
0
2 answers

One thing you can do is increment a and short as soon as you find a match. Currently you are calculating all of the possibilities before selecting out the lowest. All of that is unnecessary work when you just want the first match.

final = -1
for x in range(int(a), 1006):
  if add(x) % 4 == 0:
    final = x
    break
print(final)

Source: link

0

I'm trying a code on Hackerearth which is to Find special number. A number n is said to be special if the sum of its digits is divisible by 4 For a given integer a, find a number such that: n is a special number, n>=a, n is minimum possible. I have created a code with all my knowledge and working absolutely fine and giving correct input but it is giving Time limit exceeded error
def add(n):
    num_str = str(n)
    sum = 0
    for i in range(0, len(num_str)):
        sum += int(num_str[i])
    return sum
T=int(input())
while T>0:
    a = input()
    final=min([x for x in range(int(a),1006) if add(x)%4==0])
    print(final)
    T = T - 1
One thing you can do is increment a and short as soon as you find a match. Currently you are calculating all of the possibilities before selecting out the lowest. All of that is unnecessary work when you just want the first match.
final = -1
for x in range(int(a), 1006):
  if add(x) % 4 == 0:
    final = x
    break
print(final)

Source: link

Recent Questions on python

    Programming Languages