From the Archives
Archived discussions on CrazyEngineers

@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 !

Related Posts

Archives

@Kaustubh Katdare · Aug 6, 2013

Airtel is launching 4G services in Delhi in the next few weeks. According to telecomtiger, Huawei is most likely to handle the overall rollout plan, design, supply and deploy Airtel's...
Archives

@mahesh kumar · Aug 20, 2009

Hi, could u please explain me how to interface gps and gsm together with microcontroller(8051)
Archives

@anas mohamad · Apr 30, 2012

hey all .. it's my first post here .. i really need your help i'm a mechatronices student .. i want an ideas for the graduation project .. my partner...
Archives

@Dixsita · Sep 15, 2009

I wish to discuss the advantages of link-list over array? I've been referring to the basic C-programming books, but haven't been able to make anything out of it.
Archives

@Kaustubh Katdare · May 9, 2009

At least two reliable sources have informed that Capgemini is firing employees in its India offices. Can someone confirm this?