Need Help! my code is successfully compiled but not getting submitted due to TLE

MUFFINS3
my code

try:
    for i in range(int(input())):
        n = int(input())
        rem = 0
        c = []
        temp = 0
        for A in range(1,n+1):
            rem = n%A
            if temp <= rem:
                temp = rem
                c.append((A,temp))
            
        pkg = [p for p,q in c]
        print(max(pkg))

except: pass

I know my code is big but how to remove TLE error while submitting?
I have used try, except as codechef giving me nzec error everytime.

Hii Parth
See below My simple Code

for i in range(int(input())):
    s=input()
    x=int(s[0])
    y=int(s[len(s)-1])
    print(x+y)

See My Other solution

import math
t=int(input())
for i in range(t):
    n=int(input())
    last=n%10
    first=n//int((math.pow(10,len(str(n))-1)))
    print(first+last)

thanks for sharing the other solutions but what is wrong with my code. please tell me.

Input

3
1
2
3

Expected Output

2
4
6

Your Output

1
2
3

PS: You have neatly formatted the code despite posting for the first time. That’s cool.

2 Likes

thanks a lot

Coming to the problem with your code, I don’t really understand why you’ve used rem and all the other while loop, you ARE already converting the number to string, so you don’t need to do all those stuff. len(str(x)) == a also doesn’t make any sense to me, length of str(x) will be equal to a only in the first iteration of the loop, so it’s basically useless. Not to mention, len(str(x)) == 1 is of no use either. Since it seems like you do want to convert the number to string, this is yet another solution:

try:
    t = int(input())
    
    for i in range(t):
        x = int(input())
        s=str(x)
        sum = 0
        a = len(s)
        sum=int(s[a-1])+int(s[0])
        print(sum)
except: pass
1 Like

Your Code May Be Right But It Is Very Resource Intensive & Takes Longer Than The Allowed Limit, Hence The TLE(TIME LIMIT EXCEEDED) Error.

Here Is A Reimplemented Code:

T = int(input())
for tc in range(T):
    N = int(input())
    print((N // 2) + 1)