Self-Destructing Strings

I am getting wrong answer for this question can any1 please help where i went wrong

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

int main()
{
int t;
cin>>t;
while(t–)
{
string s;
cin>>s;
int n1=0;
int n0=0;
int n=s.size();

     for(int i=0;i<n;i++)
        {
            if(s[i]=='1')
            {
               n1++; 
            }
            else
            {
                n0++;
            }
        }
    if(n%2!=0)
    {
           cout<<-1<<endl;
    }
    else if(n0==0 || n1==0)
    {
        cout<<0<<endl;
    }
    else
    {
       
        if(n1==n0)
        {
            cout<<0<<endl;
        }
        else
        {
            int k1=max(n1,n0);
            int k2=min(n1,n0);
            int ans=(k1-k2)/2;
            cout<<ans<<endl;
        }
 
    }
  
       
  
}
return 0;

}

1 Like

Try 1111 ,ans will be -1 your ans is 0.
Here is mine.

YES
else if(n0==0 || n1==0)
{
cout<<0<<endl;
}
in this line,0 -> -1

thanx a lot bro

#include
using namespace std;

int main() {
int t,count_0=0,count_1=0,ans,i,len;
cin>>t;
for(i=0;i<t;i++)
{
string s;
cin>>s;
len=s.size();
for(i=0;i<len;i++)
{
if(s[i]==β€˜0’)
{
count_0+=1;
}
else
{
count_1+=1;
}
if(len%2 ==1 || count_0 ==0 || count_1 ==0 )
{
ans=-1;
}
else
{
ans=abs(count_1 - count_0)/2;
}
}
cout<<ans<<endl;
}
// your code goes here
return 0;
}

this is my code its shows wrong answer can any one help me

can anyone explain the question?
I misunderstood and got wa

I did the same .
i got wa.
don’t know why

There are two possible errors in your code:

  1. You are using the same variable β€˜i’ to loop through the test cases, as well as to iterate through the string.
  2. count_0 and count_1 need to be reinitialized to 0 for every test case.

Thanku so much!! it worked!!