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

int main() {
int t;
cin>>t;
while(t–){
int n;
cin>>n;
vectora(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
int o=0,z=0;
for(int i=0;i<n;i++){
if(a[i]==1){
o++;
}
else if(a[i]==0){
z++;
}
}
if(n%2==1 || o==0 || z==0 || o>z){
cout<<-1<<endl;
}
else if(o==z){
cout<<0<<endl;
}
else if(o<z){
cout<<(z-o)/2<<endl;
}
}
return 0;
}
my approach is when the number of elements is odd or array contains only zero or only ones or number of ones is greater than number of zeroes simply return -1
when count of ones equal to count of zeroes return it 0
when count of zeroes is greater than count of ones we will return as (count of zeroes-count of ones)/2
and the result showing as wrong answer
what’s the problem?

Hint

1 \oplus 1 = 0

1 Like

Hint →

1. if array contains only ones , You can create new zeroes , using 1^1 =0 .
2. If number of 1 is greater than zeroes then you can still change two 1’s to 2 zeroes each time until number of zeroes is not more or equal than 1’s .
A. if zeroes are equal to 1’s , simply anwer your count
B. else increase it by 1 , (take care for test case 11)

I hope it will helpful to You.

1 Like