A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.
import java.util.Scanner;
class next{
public static void main(String ags[])
{
Scanner in = new Scanner(System.in);
int n=in.nextInt();
for(int i=0;i<n;i++)
{
String s = in.next();
if(s.length()%2==0)
{
int p = s.length()/2;
String sub = s.substring(0,p);
String sub2= s.substring(p,s.length());
String reverse = new StringBuffer(sub).reverse().toString();
int t = Integer.parseInt(reverse);
int t1= Integer.parseInt(sub2);
if(t>t1)
{
System.out.println(sub+reverse);
}
else
{
int t2 = Integer.parseInt(sub)+1;
String s2 = Integer.toString(t2);
String rev = new StringBuffer(s2).reverse().toString();
System.out.println(s2+rev);
}
}
else
{
int len = s.length()/2;
String se1 = s.substring(0,len);
String se2 = s.substring(len+1,s.length());
String reverse = new StringBuffer(se1).reverse().toString();
int t = Integer.parseInt(reverse);
int t1= Integer.parseInt(se2);
if(t>t1)
{
String se = s.substring(0,len+1);
System.out.println(se+reverse);
}
else
{
String temp = s.substring(0,len+1);
int tn = Integer.parseInt(temp);
tn++;
String left = Integer.toString(tn);
String grip = left.substring(0,left.length()-1);
String reve = new StringBuffer(grip).reverse().toString();
System.out.println(left+reve);
}
}
}
}
}
I am a beginner to programming.I am trying to solve this question #PALIN on spoj , but I am getting runtime error even though it is working on ideone.please help me
There are a few problems with your code, but the biggest is that you're calling Integer.parseInt
on strings that can represent values far outside the range of int
. The instructions say that the input can be up to a million digits, which means that you're calling Integer.parseInt
on strings of up to half a million digits — meaning, strings of up to 10500000−1 — but int
s only go up to 231−1, which is less than 1010.
Once you've fixed that, you can test your code by doing the following:
String computeNextPalindrome(String s)
. That way, you can test that method by programmatically calling it and examining its result, rather than messing around with standard output.expectedResult
to the expected result of computeNextPalindrome("99999")
(namely "100001"
) and then iterates downward from 99999
to 1
, verifying that computeNextPalindrome
always returns expectedResult
. After each verification, check if the current number is a palindrome, and if so, update expectedResult
to the current number.That way, you can exhaustively test that your logic is correct for all numbers up to five digits. Aside from the bug I mentioned above, about trying to use int
for values well outside the range of int
, most bugs will show up for some small cases. It's rather unusual to have a bug that only affects large values; so by exhaustively testing small values, you can find almost all bugs.
You can then check for bugs specific to large values by spot-testing a few large values, like "1234567890123456789012345678901234568790"
(which should give "1234567890123456789119876543210987654321"
).