Discussion on elaborate list of reasons for NZEC error in Python

During contest I face NZEC error multiple times and when I switch to C with the same logic, code works. Can you please share what type of common mistakes you have encountered which result in Python NZEC runtime error?

For e.g. this is a practice problem: link. For which Python code (link) is giving AC for 3 cases of each subtask but NZEC for fourth. While the C code (link) gets accepted with same logic.

If you have encountered any other mistakes not relevant to problems here, please mention them too.

Other problem(link) with NZEC in Python (link) but AC in C (link).

Your logic is not the same in C and python. In the first example you’re neglecting b[i]=0 whenn assembling the array (what happens if b[i]=0 for all i? KABOOM!). In the second case you’re not using fast exponentiation in python. Your program would time out anyway, but it crashes because python cannot create a range from 1 to 10^9 (lack of memory).

1 Like

Sometimes problem with the test cases input as well,

some may give an extra space in the input not specified in the constraints

hence it may give NZEC if you are using raw_input().split(’’) etc.

Thank you for the answer, it gave me good understanding. I used to code in C and NZEC error only used to come when I missed ‘return 0’. So I was under the impression that I am missing some similar standard thing in Python too.

I guess what the judge shows you in the case of python is the way the interpreter terminated. If you have an out-of-bounds/memory error, your program will typically terminate with an exception/error, and the interpreter will signal the abnormal termination with a nonzero exit code. In contrast if a C program performs an invalid memory access it will be terminated forcefully by the OS. You will get an segfault (or maybe an abort).

1 Like

Yes, this seems to be the case. For Python, NZEC error can be generated from a very broad range of errors as compared to C. I think this will make debugging a Python solution harder as compared to C. Also I feel you should add your above comment in the beginning of your answer so that anyone coming to this post can get broader explanation for NZEC error.

True, hence the best way to take input is ‘raw_input().strip().split()’