Make Length 1, WA on test 4

Hey, I’m getting AC if I’m running code individually, but Getting WA when these test cases are running together, someone, plz help
#include
using namespace std;

int main() {
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
string s;
cin>>s;
int c=0;
if(n==1)
{
cout<<“Yes”<<endl;
}
else{
for(int i=0;i<n-1;i++)

    {
        if(s[i]=='1' && s[i+1]=='1')
        {
            s[i]='0';
            s[i+1]='0';
        }
    }
    for(int i=0;i<n;i++)
    {
        if(s[i]=='1')
        {
            c=1;
            break;
        }
    }
   }
    if(c==0)
    {
        cout<<"YES"<<endl;
    }
    else
    {
        cout<<"NO"<<endl;
    }
    
}
return 0;

}

Can you explain in brief, your approach? So that someone looking at the code may understand what are you doing .

When the string was of size 1, you were print Yes two times.
You should have put a continue in if(n==1) block.
Here is the Accepted Code.

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

int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
cin>>s;
int c=0;
if(n==1)
{
cout<<"Yes"<<endl;
continue;
}
else{
for(int i=0;i<n-1;i++)

    {
        if(s[i]=='1' && s[i+1]=='1')
        {
            s[i]='0';
            s[i+1]='0';
        }
    }
    for(int i=0;i<n;i++)
    {
        if(s[i]=='1')
        {
            c=1;
            break;
        }
    }
   }
    if(c==0)
    {
        cout<<"YES"<<endl;
    }
    else
    {
        cout<<"NO"<<endl;
    }
    
}
return 0;

}

When I would have to explain the importance of code indentation to someone, I am going to show them this example.

1 Like

thanks bhaiya, got it.

1 Like