CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@sookie • 24 Aug, 2009
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: Code Chef

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:
2
808
2222
818
2332
Thanks in advance for finding out at what case, it can give runtime error !
3.6k views

Related Posts

@ShArMaT · Jul 4, 2010

I'm interested in Ethical Hacking and wish to do my engineering project related to it. I'm currently in final year and want to know if there are any ideas I...
17.4k views

@Ankita Katdare · Jan 7, 2011

Many of you will be appearing for GATE 2011. All with high aspirations for doing the M.Tech from a good college. To help others choose better, share your opinions on...
3.1k views

@xheavenlyx · May 9, 2011

On an infinite grid of ideal one ohm resistors, what is the equivalent resistance across the two given points? A clearer image:
3.1k views

@Kaustubh Katdare · Mar 30, 2011

Hero Octane Recra 26T is 21 speed bike by Hero Cycles - the largest manufacturers of bikes in the world. If you're looking to buy this bicycle, take a look...
22.1k views

@pankaj12790 · Sep 30, 2013

I just filled up the iiast form for admission in iia Bangalore ..... Do someone know the material I could use for this exam ... Thank u in advance 😀
5.7k views