I think you can add something on how to avoid those leading zeroes. I was simple printing the array and wasted A HUGE CHUNK of my time because it was printing leading 0.
And BTW, I feel the problem statement SHOULD specify that leading zeroes arent allowed. I mean, yeah, to many people “5555+5555=0000” would make sense.
That ought to be the case, but i thought of real life calculators, which never print 0000. Maybe the author’s calculator wasn’t that faulty to print 0000.
Possible XD. Imagine my reaction when I opened comment section after 20min only to realzie that leading zeroes are giving WA. I was like, “… (xinfinity)XDXD”
t = int(input())
while t>0 :
n = input()
i = len(n)
flag = 0
li = []
while i>0:
copy = int(n[:i-1] + n[i:])
if copy%6 == 0:
li.append(copy)
flag = 1
i -= 1
if flag == 1:
print(str(max(li)).rjust(len(n)-1, "0"))
else:
print("-1")
t = t-1
I don’t know why it was giving TLE although you have also used nested loops and hence O(n^2)… please help me?
@taran_1407 the very first solution that passed is O(N^2)! Can you please look at his code and please comment that whether the test cases are weak or he has made some optimization ?
just remove the line where you have checked the value of p and AC
the idea is that when you get the value lesser, in the position nearer to the units place it will lead to better solution than that in the higher digits !