Program to find out Next Palindrome
Hi all programmers here!
Problem Statement :
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.
Input
The first line contains integer t, the number of test cases. Followed by t lines containing integers K.
Output
For each K, output the smallest palindrome larger than K.
Example
Input:
2
808
2133
Output:
818
2222
Warning: large Input/Output data, be careful with certain languages
Source: <a href="https://www.codechef.com/problems/PALIN" target="_blank" rel="nofollow noopener noreferrer">PALIN Problem | CodeChef</a>
For above Problem, I created following program and also even submitted at the site mentioned in Source but their machine is again and again giving some Runtime error. As per those folks, it is because I am doing some wrong in I/O handling but as per me , it is working fine in my machine. I am not able to trace it out. 😔 Anyone here any ideas what error could be?
My Program:
Problem Statement :
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.
Input
The first line contains integer t, the number of test cases. Followed by t lines containing integers K.
Output
For each K, output the smallest palindrome larger than K.
Example
Input:
2
808
2133
Output:
818
2222
Warning: large Input/Output data, be careful with certain languages
Source: <a href="https://www.codechef.com/problems/PALIN" target="_blank" rel="nofollow noopener noreferrer">PALIN Problem | CodeChef</a>
For above Problem, I created following program and also even submitted at the site mentioned in Source but their machine is again and again giving some Runtime error. As per those folks, it is because I am doing some wrong in I/O handling but as per me , it is working fine in my machine. I am not able to trace it out. 😔 Anyone here any ideas what error could be?
My Program:
package code.chef; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * @author sookie */ public class NextPalindrome { public static void main(String[] args) throws IOException { int t, k; StringBuilder z; BufferedReader bin = new BufferedReader(new InputStreamReader(System.in)); t = Integer.parseInt(bin.readLine()); String inputs[] = new String[t]; for(int i = 0; i < t; i++) { inputs[i] = bin.readLine(); } for (int i = 0; i < t; i++) { k=Integer.parseInt(inputs[i]); do{ z=new StringBuilder(); k=k+1; z.append(k); z=z.reverse(); if(Integer.parseInt(z.toString())==k){ System.out.println(k); break; }else continue; }while(k>=1); } } }Output:
2Thanks in advance for finding out at what case, it can give runtime error !
808
2222
818
2332
0