CHARGES - Editorial

why is your array size k+1 instead of n+1?

try change cout << total << endl; to cout << total << ‘\n’;

void solve() {
    int n,q;cin>>n>>q;
    string s;cin>>s;
    int pre = 0;
    for(int i = 0;i<n-1;i++){
        if(s[i] == s[i+1])pre+=2;
        else pre+=1;
    }
    while(q--){
        int k;cin>>k;
        k--;
        if(s[k] == '0')s[k] = '1';
        else s[k] = '0';
        if(k == 0){
            if(s[k] == s[k+1])pre++;
            pre--;
        }
        else if(k == n-1){
            if(s[k] == s[k-1])pre++;
            pre--;
        }
        else{
            if(s[k] == s[k+1] and s[k] == s[k-1])pre+=2;
            if(s[k] != s[k+1] and s[k] != s[k-1])pre-=2;
        }
        cout << pre << endl;
    }
}

can some help me out what’s wrong with this code please?

No use :frowning: bro
Is there any problem with code or approach!!!?

man, you messed up the if conditions. Also n == 1, only need to print 0 once. you printed multiple times

Accepted : CodeChef: Practical coding for everyone

I have ran Darshan Lokhande’s exact code given in his video editorial… It gives wrong answer…
@darshancool25 bhaiya…Please help…
My submission:
https://www.codechef.com/viewsolution/47293053

sorry, but i didn’t understand where i messed up in my if condition. Can you tell me a test case where it fails, because I checked with many test cases and it works fine.

Maybe

have a look at some other’s code i fixed
https://www.codechef.com/viewsolution/47293393
https://www.codechef.com/viewsolution/47292653
https://www.codechef.com/viewsolution/47290474
https://www.codechef.com/viewsolution/47291109

if(kk>=0 && kk<=n-1) { // you wrap the condition i == 0 and i == n - 1 inside this if, which will cause issue, better seperate into three conditions (i == 0, i == n - 1) and else 0 < i < n - 1, as you can see the fixed code
if(s[kk]==‘0’)
s[kk] = ‘1’;
else
s[kk] = ‘0’;
if(kk==0){
if(s[kk]==s[kk+1]) dis+=1;
else dis-=1;
cout<<dis<<endl;
continue;
}
if(kk==n-1){
if(s[kk]==s[kk-1]) dis+=1;
else dis-=1;
cout<<dis<<endl;
continue;
}
if(s[kk]==s[kk-1]){
dis+=1;
}else{
dis-=1;
}
if(s[kk]==s[kk+1]){
dis+=1;
}else{
dis-=1;
}
cout<<dis<<"\n";
}


at least failed this edge case when string length is 1
1
1 3
0
1 1 1

should output:
0

you code
0
0
0

please check @darshancool25 why my code giving me wa.
(CodeChef: Practical coding for everyone)

Hey, thanks for your help, there are some points, that I found right now:
I shifted all my code within while loop into a function and called this function into while loop and now it works.
2.So, the logic is correct and there must be no issue within any conditionals.
3. I think 0 0 0 is not an issue, because my accepted solution gives o/p 0 0 0 for this test case.
4.What can be the possible issue with this…

Well, I got the issue, In my solution, in the condition if(n==1) : I was using return, instead I had to use continue there.
Replacing that return with continue got it AC.

if(s[q]=='0') s[q]='1';
else s[q]='1';

you messed up here :sweat_smile:

1 Like

Because you are skipping the input of all K queries for cases where N==1. Also take the initial input into a string, rather than character array. :slight_smile:

Can someone please find the error its giving rutime error .why?
@darshancool25
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
int t;
cin>>t;
while(t–)
{
ll n,k;
cin>>n>>k;
string s ;
cin>>s;

    if(n==1)
    cout<<0<<endl;
    if(n>1){
    ll initial=0;
    for(ll i=0;i<s.size()-1;i++)
    {
        if(s[i] == s[i+1])
        initial+=2;
        else
        initial+=1;
    }
    //cout<<"Initial length of the string is "<<initial<<endl;
    ll i=0;
    int p;
    while(i<k)
    {
        //int p;
        cin>>p;
        int a=p-1;
        if(s[p-1] == '0')
        //s[p-1]='1';
        //int a = p-1;
        s.replace(a,1,"1");
        else
        //s[p-1]='0';
        s.replace(a,1,"0");
        //cout<<"Now the string is "<<s<<endl;
        if(p==n)
        {
            if(s[p-1] == s[p-2])
            {
                initial+=1;
            }
            else
            initial-=1;
        }
        else if(p==1)
        {
            if(s[p-1] == s[p])
            {
                initial+=1;
            }
            else
            initial-=1;
        }
        else
        {
            if(s[p-1] == s[p-2] && s[p-1]==s[p])
            initial+=2;
            else if((s[p-1]==s[p-2] && s[p-1]!=s[p]) || (s[p-1]!=s[p-2] && s[p-1]==s[p]))
            {
                continue;
            }
            else
            {
                initial-=2;
            }
        }
        cout<<initial<<endl;
        i++;
    }
    }
}
return 0;

}

Thanks a lot for pointing out my mistake.

#include
using namespace std;

int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t–){
long long int n,k;
cin>>n>>k;
if(n==1){
cout<<“0”<<"\n";
continue;
}
string s;
cin>>s;
long long int sum=0;
for(long long int i=0;i<=n-2;i++){
if(s[i]!=s[i+1])
sum=sum+1;
else
sum=sum+2;
}

    for(long long int i=0;i<k;i++){
        long long int a;
        cin>>a;
        if(a==1){
           if(s[0]==s[1])
             sum=sum-1;
           else
            sum=sum+1;
        }
       else if(a==n){
           if(s[n-2]==s[n-1])
             sum=sum-1;
           else
             sum=sum+1;
       }
       else{
          if(s[a-1]==s[a-2])
            sum=sum-1;
       	  else
       	   sum=sum+1;
       	   
       	  if(s[a-1]==s[a])
       	    sum=sum-1;
       	  else
       	    sum=sum+1;
           
       }
       if(s[a-1]=='0')
         s[a-1]='1';
        else
         s[a-1]='0';
       
       cout<<sum<<"\n";
           
    }
    
    
}
return 0;

}

Can somebody tell me what is wrong with this code on submission?

`#include<bits/stdc++.h>

using namespace std;

#define loop(i,n) for(int i=0;i<n;i++)
#define ll long long
#define scan(arr) for(auto &it:arr)cin>>it;
#define print(a) {for(auto it:a)cout<<it<<" ";cout<<endl;}

int main() {
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
string s;
cin>>s;
int dist=0;
// cout<<s<<endl;
for(int i=1;i<n;i++)
{
if(s[i]!=s[i-1])
dist++;
else
dist+=2;
}
// cout<<s<<endl;
for(int i=1;i<=k;i++)
{
int q;
cin>>q;
if(q==1&&s[q]==s[q-1])
{
dist–;
}
else if(q==1&&s[q]!=s[q-1])
{
dist++;
}
else if(q==n&&s[q-1]!=s[q-2])
{
dist++;
}
else if(q==n&&s[q-1]==s[q-2])
{
dist–;
}
else if(s[q-1]!=s[q]&&s[q-1]!=s[q-2])
{
dist+=2;
}
else if(s[q-1]==s[q]&&s[q-1]==s[q-2])
{
dist-=2;
}

        if(s[q-1]=='1')
            s[q-1]='0';
        else
            s[q-1]='1';
       // cout<<s<<endl;
       cout<<dist<<endl;
    }
}
return 0;

}`

Can someone help me out with this code??
It’s passing many test cases but I cant figure out where it fails.

https://www.codechef.com/viewsolution/47308647
GUYS please help me find out mistake ,i have tried a lot but still not getting whats wrong .
I have checked almost all cases including n=1 and getting correct answer , but still WA .Please help me out

My code always getting runtime error :

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
int t;
cin>>t;

while(t--)
{
    int n,k;
    cin>>n>>k;
    
    string s;
    cin>>s;
    
    
    
    
    
    int d = 0;
    
    for(int z=0;z<=s.size()-2;z++)
        {
           if(s[z]==s[z+1])
           d+=2;
           
           else
           d+=1;
        }
        
       int len = 0;
       
       
       
    while(k--)
    {
        int j;
        cin>>j;
        j--;
        
        
        
        if(s[j] =='1')
        s[j] = '0';
        
        else
        s[j] = '1';
        
        
        if(j>0)  //left
        {
        if(s[j-1]==s[j])
        len++;
        
        else 
        len--;
        }
        
        
        if(j<s.size()-1)  //right
        {
        if(s[j+1]==s[j])
        len++;
        
        else
        len--;
        }
        
        d = d + len;
        cout<< d <<"\n";
        
        
        len = 0;
        
    }
    
    
    
}
return 0;

}