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(System.in);
	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;
	            	else
	                	coins[j] = 1;
	        	}
	        }
	        int count = 0;
	        for (int i=0; i<rounds; i++){
	            if(coins[i] == target)
	                count += 1;
	        }
	        System.out.println(count);
	    }
	}
	
}

}

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

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

The problem link is https://www.codechef.com/problems/CONFLIP . 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. :slight_smile:

I would also suggest you take a look at this - http://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/

1 Like

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. https://www.codechef.com/viewsolution/14531353

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.

2 Likes