Help me in solving AOCV215 problem

My issue

What’s the need to introduce “count_zero” variable, as given in the solution.
Even if there is a single 0 in the array, the product is non-negative.

My code

// Solution as follows

#include <bits/stdc++.h>
using namespace std;

int main() 
{
	int t;
    cin >> t;
	
	while(t--)
	{
	    int N;
	    cin >> N;
	    int A[N];
	    for(int i = 0; i < N; i++)
	    {
	        cin >> A[i];
	    }
	    int count_neg = 0;
        for(int i = 0; i < N; i++)
        {
            if(A[i] == 0)
            {
              cout<<0<<endl;
            }
            if(A[i] < 0)
            {
                count_neg++;
            } 
        }
     
        if( (count_neg%2 == 0))
        {
            cout << 0 << endl;
        }
      
        else
        {
            cout << 1 << endl;
        }
	}
}	

Learning course: C++ for problem solving - 2
Problem Link: CodeChef: Practical coding for everyone

Yes, you are correct. If there’s a single 0 the product will be non-negative and we just need to print 0. But how do we keep track of whether or not we encountered a 0? That’s what the variable is for, and that’s the reason we introduce that variable.

BTW this is debugging and you need to correct the logical error in the code. If you are not sure, the answer is to replace

if((count_neg%2 == 0))

by

if((count_neg%2 == 0) || (count_zero != 0))

Signifying that if there’s even a single 0 in array, answer is 0

1 Like