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


Run time error NZEC.

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

import java.util.*;

class coins

    public static void main (String[] args)

    Scanner in = new Scanner(;
    int test;
    int numOfGames;
    int initial;
    int rounds;
    int target;
    //System.out.println("Enter the number of test cases:");
    test = in.nextInt();

        while (test-- != 0){
        //System.out.println("Enter the number of games:");
        numOfGames = in.nextInt();
        while (numOfGames-- != 0){
            //System.out.println("Enter initial, rounds and target:");

            initial = in.nextInt();
            rounds = in.nextInt();
            target = in.nextInt();

            int[] coins = new int[rounds];
            for (int i=0; i<rounds; i++){
                coins[i] = initial;
            for (int i=0; i<rounds; i++){
                for (int j=0; j<=i; j++){
                    if (coins[j] == 1)
                        coins[j] = 2;
                        coins[j] = 1;
            int count = 0;
            for (int i=0; i<rounds; i++){
                if(coins[i] == target)
                    count += 1;



Its showing runtime error NZEC. Can someone please check my code? i'm new to codechef.

asked 19 Jul '17, 20:22

sauravsuman001's gravatar image

accept rate: 0%

edited 19 Jul '17, 20:27

Give the problem link as well please. Its not possible to judge a solution before knowing the question.

(19 Jul '17, 21:26) vijju123 ♦♦5★

The problem link is . You are getting NZEC (which means runtime error) because the max input value for N (here rounds) is 10^9 . You are therefore trying to create an array with 10^9 elements, which is not allowed in java. The max number of elements that the compiler will allow is 10^9, but in reality, whenever the number of elements is > around 4.4*10^7, java will give a runtime error.

So, try to limit the number of elements in an array to 10^7. Hope that answered your question. :)

I would also suggest you take a look at this -


answered 20 Jul '17, 01:23

akashbhalotia's gravatar image

accept rate: 10%

edited 20 Jul '17, 01:31

In this problem, you don't even need to create an array. It's a very simple logic

Let's say we have N number of rounds to be an even number. For example - 4 Now we know either they all have to be tail or head. Let's say we have head

{H H H H}

Now the logic is if we have N to be even number, we divide it by 2 which is The answer will be (N/2) So the above head will end up be

{H T H T}

so when we have an even number of rounds and coins, it doesn't matter if the coin is head or tail

The problem comes up when we have odd numbers of rounds and coins. For example, we have 5 number of rounds and coin. This time let's say we have tail

{T T T T T}

Now the result will be

{H T H T H}

and we need to find the number of tails in the result Here's the logic if the given coins are the tail and an odd number of coins and rounds. The result will be (N/2)+1 if we're finding the same coin in the result as the coin given in the question. if we had to find the Head in the above question then we simply do (N/2). I hope that helps, you can check out my solution for this problem.


answered 20 Jul '17, 03:23

kunnu120's gravatar image

accept rate: 5%


He knows this. See his submissions. He asked this question after successfully submitting using the correct logic. He just wanted to know what was wrong with his above approach.

(20 Jul '17, 15:24) akashbhalotia5★
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: 19 Jul '17, 20:22

question was seen: 388 times

last updated: 20 Jul '17, 15:24