Getting NZEC Runtime Error, even when code Runs fine in Codechef IDE

I solved a problem, URL: https://www.codechef.com/problems/IEMCO8E/

While running on Codechef IDE with custom inputs given this code runs fine with status ‘SUCCESSFULY EXECUTED’, but after submission this gives a ‘Runtime Error NZEC’!

Why so?
What modifications in the code are required?

Code given here:

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }

    static class Pair<S extends Comparable<S>, T extends Comparable<T>> implements Comparable<Pair<S, T>> {
        S first;
        T second;

        Pair(S f, T s) {
            first = f;
            second = s;
        }



        @Override
        public int compareTo(Pair<S, T> o) {
            int t = first.compareTo(o.first);
            if (t == 0) return second.compareTo(o.second);
            return t;
        }

        @Override
        public int hashCode() {
            return (31 + first.hashCode()) * 31 + second.hashCode();
        }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof Pair)) return false;
            if (o == this) return true;
            Pair p = (Pair) o;
            return first.equals(p.first) && second.equals(p.second);
        }

        @Override
        public String toString() {
            return "Pair{" + first + ", " + second + "}";
        }
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        Reader rd = new Reader() ;
        int n = rd.nextInt() ;
        Pair<Long,Long> p[] = new Pair[2*n] ;

        String[] lr, xy;
        for(int i=0;i<n;++i){
            lr = rd.readLine().split(" ") ;
            p[2*i] = new Pair<>(Long.parseLong(lr[0]),(long)1) ;
            p[2*i+1] = new Pair<>(Long.parseLong(lr[1])+1,(long)-1) ;
        }

        xy = rd.readLine().split(" ") ;
        int x=Integer.parseInt(xy[0]),y=Integer.parseInt(xy[1]) ;
        Arrays.sort(p);

        long c=0,cnt=0,prev=-1, ans=0;
        for(int i=0;i<2*n;++i){
            c=p[i].first ;
            if(cnt>=x &&  cnt<=y) ans+=(c-prev) ;
            cnt += p[i].second ;
            prev = c;
        }
        System.out.println(ans);

 }//main end
    }//class end

Kindly help me find out the reason for the same.
Thanks in Advance.

Leaving aside the issue of whether your program is correct or not, I can’t think of anything that would give a Runtime Error unless there is some dodgy whitespace in the test inputs which, apparently, is quite often the case :slight_smile: For example, a space at the beginning of a line containing L-R or X and Y, or a double-space between L and R or X and Y will cause a crash.

Try with this:

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }

    static class Pair<S extends Comparable<S>, T extends Comparable<T>> implements Comparable<Pair<S, T>> {
        S first;
        T second;

        Pair(S f, T s) {
            first = f;
            second = s;
        }



        @Override
        public int compareTo(Pair<S, T> o) {
            int t = first.compareTo(o.first);
            if (t == 0) return second.compareTo(o.second);
            return t;
        }

        @Override
        public int hashCode() {
            return (31 + first.hashCode()) * 31 + second.hashCode();
        }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof Pair)) return false;
            if (o == this) return true;
            Pair p = (Pair) o;
            return first.equals(p.first) && second.equals(p.second);
        }

        @Override
        public String toString() {
            return "Pair{" + first + ", " + second + "}";
        }
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        Reader rd = new Reader() ;
        int n = rd.nextInt() ;
        Pair<Long,Long> p[] = new Pair[2*n] ;

        for(int i=0;i<n;++i){
            p[2*i] = new Pair<>(rd.nextLong(),(long)1) ;
            //System.out.println("i: " + i + " blah even: " + p[2 * i]);
            p[2*i+1] = new Pair<>(rd.nextLong() + 1,(long)-1) ;
            //System.out.println("i: " + i + " blah odd: " + p[2 * i + 1]);
        }

        int x=rd.nextInt(),y=rd.nextInt() ;
        //System.out.println("x: " + x + " y: " + y);
        Arrays.sort(p);

        long c=0,cnt=0,prev=-1, ans=0;
        for(int i=0;i<2*n;++i){
            c=p[i].first ;
            if(cnt>=x &&  cnt<=y) ans+=(c-prev) ;
            cnt += p[i].second ;
            prev = c;
        }
        System.out.println(ans);

 }//main end
    }//class end

2 Likes

It worked, thanks @ssjgz , got the correct answer prompt, credits to you too…:grinning:for the quick help.

Can you please tell me how we could take space separated(unknown number of spaces, as was the case here for error, and you got it right) i/p in JAVA without using this hard to remember ‘Reader class’?

1 Like

Sorry, I’m not a Java programmer so I can’t really advise :confused: I think most people just have a code template that they copy and paste from, so it doesn’t matter so much if the code is “hard to remember” :slight_smile:

Edit:

Also, maybe have a browse through the AC Java solutions - someone presumably is using a more elegant way.

1 Like

You are correct by the way :smile:

Can we copy and paste in contests like ACM ICPC or google hash code etc…?(i literally don’t know it clearly)

You definitely can copy and paste snippets in Codechef and Hackerrank (though obviously, take care not to plagiarise solutions!), but I don’t know about other contests.

1 Like

OK, thanks for such quick solutions to my questions.

1 Like