Runtime Error(NZEC)

Why am I getting this error for my code on the problem The Next Palindrome (http://www.codechef.com/problems/PALIN)

import java.io.*;
public class Main
{

    public static void main(String[] args)throws IOException
    {
        int i;
        long k;
        int j;
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);
        System.out.println("Input:");
        i=Integer.parseInt(br.readLine());
        long arr[]=new long[i];

        for(j=0;j<i;j++)
        {
            arr[j]=Long.parseLong(br.readLine());       
        }
        System.out.println("Output:");
        for(j=0;j<i;j++)
        {
            System.out.println(Palindrome(arr[j]));     
        }
    }

    public static long Palindrome(long k)
    {
        long n=k+1;
        while(true)
        {
            if(pal(n))
                return n;
            n=n+1;
        }
    }

    public static boolean pal(long n)
    {
        long n2=n;
        long sum=0;
        long dig;
        while(n2>0)
        {
            sum=sum*10;
            dig=n2%10;
            sum+=dig;
            
            n2=n2/10;
        }
        if(sum==n)
            return true ;
        else
            return false;
    }
}

The integer K in the problem statement has upto 1000000 digits. You cannot save it in long (as you are trying to do). That causes RTE.

1 Like

Also you don’t have to print useless stuff like “Input:”
or “Output:”. That would get you WA even if your implementation was right.

1 Like

Oh ,so what do I save it in,isnt long the largest data type in Java.Also,when you say not to print ,what exactly do I have to output then?

@shantanujswl Use Strings to save the numbers, or Biginteger (If the time limit is not very strict). You just have to print the answer to each case. That’s it. No need to print “Input:” or “Output:”. They were used in the problem statement to differentiate the input part from the output part

1 Like

Thank you,new to codechef

//