I'm pretty new to programming and after learning the basics, I tried doing some SPOJ problems. Turns out most of my programs are inefficient and time out even though they are correct (I think)
For example, the following program takes n as number of test cases and prints the next largest palindrome.
n = int(raw_input())
a=[0]*n
for i in range (0,n):
a[i]=int(raw_input())
for i in range (0,n):
var=a[i]+1
flag=False
varstr=str(var)
while flag==False:
if varstr==(varstr[::-1]):
flag=True
print var
break
else:
var+=1;
I'm getting the right outputs, but the time is always the problem.
How do I make my code more efficient? Is there something I'm missing?