I have written the following code for the problem:
Alice has an array of NN integers — A_1, A_2, ..., A_N. She wants the product of all the elements of the array to be a non-negative integer. That is, it can be either 00 or positive. But she doesn't want it to be negative.
To do this, she is willing to remove some elements of the array. Determine the minimum number of elements that she will have to remove to make the product of the array's elements non-negative.
Code :
#include <iostream>
using namespace std;
int main() {
// your code goes here
int t;cin>>t;
while(t--){
int n;cin>>n;
int res=0;
bool flag=false;
for(int i=0;i<n;i++){
int f;cin>>f;
if(f<0){
res++;
}
if(f==0){
flag=true;
break;
}
}
if(res%2==0 || flag==true){
cout<<0<<endl;
}
else{
cout<<1<<endl;
}
}
return 0;
}
I know that if I remove the break statement, the code will get correct answer for all test cases. But why can I not use break, as it will only terminate the for loop?