You are not logged in. Please login at www.codechef.com to post your questions!

×

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;
    }
}

asked 21 Jun '13, 23:18

shantanujswl's gravatar image

shantanujswl
111
accept rate: 0%

edited 22 Jun '13, 08:41

argonaut's gravatar image

argonaut
8913920


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.

link

answered 21 Jun '13, 23:24

aichemzee's gravatar image

aichemzee
42361224
accept rate: 3%

1

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

(21 Jun '13, 23:26) aichemzee

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?

(21 Jun '13, 23:58) shantanujswl
1

@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

(22 Jun '13, 00:25) aichemzee

Thank you,new to codechef

(22 Jun '13, 07:36) shantanujswl
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×39
×36

Asked: 21 Jun '13, 23:18

Seen: 208 times

Last updated: 22 Jun '13, 08:41