Link to the problem L56GAME
#include <bits/stdc++.h>
#include
using namespace std;
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int odd=0,even=0;
while(n--){
int a;
cin>>a;
if(a%2==0) even++;
else odd++;
}
if(odd%2==0) cout<<"1"<<"\n";
else if(odd==n || even ==n) cout<<"1"<<"\n";
else cout<<"2"<<"\n";
}
return 0;
}
Do below steps
- “else if” is useless as n will always be -1 (you are decreasing n-- in while loop)
- replace while by for loop
- remove “else if” and modify “if” like : if(odd%2==0 || n == 1)
Here is my solution : https://www.codechef.com/viewsolution/18639624
It is same as yours after fixing above issues.
Yes you are missing in the logic part as well as coding part…
Consider n=3 and elements be { 1, 3 ,5} then
Your logic will give output 1 while…
taking
1+3=4
array becomes
{4 , 5 }
which can’t be reduced to anything and
#hence Output should have been 2
#when
odd%2==1 and (even>0 or odd>=3)
#then and only then output will be 2… otherwise output is 1…
avoid using while(n–) type code when you need to use value of n inside or after the while loop
Because while(n–) will erase the value of n…
#HERE is the accepted solution of what I explained to you in case you need reference…
#why the condition should be like this ??
Well take different cases and figure out the answer… understanding this is kept as homework for you… Do let me know if u still need explanation or u have any doubt on this…
Thanks for the explanation. Got AC.