import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.event.AncestorEvent;
public class NextPalindrome {
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
ArrayList<BigInteger> inArr = new ArrayList<BigInteger>();
int tc = console.nextInt();
for(int x =0 ;x<tc;x++){
BigInteger num = console.nextBigInteger();
num = num.add(BigInteger.valueOf(1));
inArr.add(num);
}
for(int y =0 ;y<tc;y++)
{
while(!checkPal(inArr.get(y))){
BigInteger temp = inArr.get(y);
temp = inArr.get(y).add(BigInteger.valueOf(1));
inArr.set(y, temp);
}
System.out.println(inArr.get(y));
}
}
static boolean checkPal(BigInteger num){
String str = num.toString();
char[] charStr = str.toCharArray();
//System.out.println(charStr);
boolean isPal= true;
int first =0;
int last = str.length()-1;
for(int i = 0; i<str.length();i++){
if(charStr[first] == charStr[last])
{
first++;
last--;
continue;
}else
{
isPal = false;
break;
}
}
return isPal;
}
}
This piece of code which i have written works fine for smaller to mid sized inputs but if i give a bigger number like any random bigger number it says time limit exceeded.Can any one give inputs regarding this.