i took the div 3 august cookoff test and tried a solution for the problem - CLOSCHEF
my submission was incorrect(wrong answer). after going through the editorial i simply cut my code -
if(un>1){cout<<“0”<<endl;goto end;}
out of the for loop and right before the first if after the for loop.
This is the wrong code. on moving the line of checking ‘un>1’ out of the loop and right before the first if outside, the program in practice submitted successfully. terminating the loop for a test case early by the below code (having the jump statement inside for) i thought should save a bit of time and should in theory be the same as if this line is outside the loop (we are printing 0 and moving on to next case, ignoring any other inputs of the array we get) . can anyone help identify the issue as i couldnt… thanks
below is the incorrect answer code. moving the line if(un>1) outside the for loop and before the first if fixes the code. please point me out why this happens.
#include
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
long long int n;cin>>n;
long long int num;
long long int no=0,po=0,un=0,z=0;
if(n==1){cout<<"1"<<endl;goto end;}
for(long long int i=0;i<n;i++){
cin>>num;
if(num>1 || num<-1)un++;
if(un>1){cout<<"0"<<endl;goto end;}
if(num==1)po++;
else if(num==-1)no++;
}
if(no!=0 && un!=0){cout<<"0"<<endl;goto end;}
if(no>1 && po==0){cout<<"0"<<endl;goto end;}
cout<<"1"<<endl;
end:;
}
return 0;
}