Answers to: Run time error NZEC.https://discuss.codechef.com/questions/105929/run-time-error-nzec<p>/<em> package codechef; // don't place package name! </em>/</p>
<pre><code>import java.util.*;
class coins
{
public static void main (String[] args)
{
Scanner in = new Scanner(<a href="http://System.in">System.in</a>);
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);
}
}
}
</code></pre>
<p>}</p>
<p>Its showing runtime error NZEC.
Can someone please check my code?
i'm new to codechef.</p>enThu, 20 Jul 2017 15:24:26 +0530Comment by akashbhalotia on kunnu120's answerhttps://discuss.codechef.com/questions/105929/run-time-error-nzec#106013<p>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.</p>akashbhalotiaThu, 20 Jul 2017 15:24:26 +0530https://discuss.codechef.com/questions/105929/run-time-error-nzec#106013Answer by kunnu120https://discuss.codechef.com/questions/105929/run-time-error-nzec/105967<p>In this problem, you don't even need to create an array. It's a very simple logic</p>
<p>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</p>
<p>{H H H H} </p>
<p>Now the logic is if we have N to be <strong>even</strong> number, we divide it by 2 which is The answer will be <strong>(N/2)</strong>
So the above head will end up be</p>
<p>{H T H T}</p>
<p>so when we have an even number of rounds and coins, it doesn't matter if the coin is head or tail </p>
<p>The problem comes up when we have <strong>odd</strong> numbers of rounds and coins. For example, we have 5 number of rounds and coin.
This time let's say we have tail </p>
<p>{T T T T T}</p>
<p>Now the result will be </p>
<p>{H T H T H}</p>
<p>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 <strong>(N/2)+1</strong> 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 <strong>(N/2)</strong>. I hope that helps, you can check out my solution for this problem. <a href="https://www.codechef.com/viewsolution/14531353">https://www.codechef.com/viewsolution/14531353</a></p>kunnu120Thu, 20 Jul 2017 03:23:17 +0530https://discuss.codechef.com/questions/105929/run-time-error-nzec/105967Answer by akashbhalotiahttps://discuss.codechef.com/questions/105929/run-time-error-nzec/105961<p>The problem link is <a href="https://www.codechef.com/problems/CONFLIP">https://www.codechef.com/problems/CONFLIP</a> . 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.</p>
<p>So, try to limit the number of elements in an array to 10^7. Hope that answered your question. :)</p>
<p>I would also suggest you take a look at this - <a href="http://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/">http://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/</a></p>akashbhalotiaThu, 20 Jul 2017 01:23:04 +0530https://discuss.codechef.com/questions/105929/run-time-error-nzec/105961Comment by vijju123 on sauravsuman001's questionhttps://discuss.codechef.com/questions/105929/run-time-error-nzec#105941<p>Give the problem link as well please. Its not possible to judge a solution before knowing the question.</p>vijju123Wed, 19 Jul 2017 21:26:24 +0530https://discuss.codechef.com/questions/105929/run-time-error-nzec#105941