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


Why does this code throw NZEC?

I've been racking my brain over this for hours and I can't figure out why I keep getting NZEC when submitting to CodeChef. I am generally good with these problems, but this one escapes me. The code runs fine locally:

package CodeChef.LeftRightTree;

import java.math.BigInteger;
// @link:
public class Main {
    public static void main(String args[]) throws Exception {
        int numTurns;
        int currentLine = 0;
        String line;
        char direction;
        boolean isEven;
        BigInteger currentNode;
        BigInteger mod = new BigInteger("1000000007");

        BufferedReader buffer = new BufferedReader(new InputStreamReader(;
        PrintWriter out = new PrintWriter(System.out);

        line = buffer.readLine();
        numTurns = Integer.parseInt(line);

        while (currentLine++ < numTurns) {
            currentNode = new BigInteger("1");

            line = buffer.readLine();

            for (int i = 0; i < line.length(); i++) {
                direction = line.charAt(i);
                isEven = currentNode.mod(new BigInteger("2")).equals(BigInteger.ZERO);

                currentNode = currentNode.multiply(new BigInteger("2"));

                if(isEven){ // node ID is even
                    currentNode = currentNode.add(new BigInteger("-1"));

                if(direction == 'r'){
                    currentNode = currentNode.add(new BigInteger("2"));



asked 27 Nov '14, 08:18

ghurtado's gravatar image

accept rate: 0%

Refer to this link HERE

This answer is marked "community wiki".

answered 27 Nov '14, 19:48

namankumar's gravatar image

accept rate: 0%

Sorry, but that's not helpful at all. The answer you linked to only gives the definition of NZEC, which is very basic and I already knew.

(28 Nov '14, 10:46) ghurtado0★

And you tried your code with 5 (test cases) x 105 digits long string with all 'l' (L characters only)?

Tip: write a program to generate such input file and the use it for testing ;-)


answered 27 Nov '14, 16:16

betlista's gravatar image

3★betlista ♦♦
accept rate: 11%

edited 27 Nov '14, 16:17

In the end the issue was embarrassingly simple: I had left in the package declaration.

package CodeChef.LeftRightTree;

After removing it, my program runs.

Unfortunately, it looks like I can't accept my own answer, but this should definitely be part of the NZEC FAQ (if one exists).

This answer is marked "community wiki".

answered 28 Nov '14, 10:44

ghurtado's gravatar image

accept rate: 0%

edited 28 Nov '14, 10:44

toggle preview

Follow this question

By Email:

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



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "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
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 27 Nov '14, 08:18

question was seen: 873 times

last updated: 28 Nov '14, 10:46