CHARGES - Editorial

@darshancool25 Please help me out

If you have watched the official video editorial, making changes in your code shouldnt be tough !! To answer your second question, just keep practicing variety of problems. Most of the problems are new to everyone, but the one having more practice can link it with a standard problem or past problem / idea.

1 Like

what’s wrong with my code pls… help-
#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
string s;
cin>>s;
int q[k];
for(int i=0;i<k;i++)
{
cin>>q[i];
}
long long int count=0;
if(n==1)
{
cout<<“0”<<"\n";

    }
    else{
    for(int i=0;i<n-1;i++)
    {
        if(s[i]==s[i+1])
        {
            count+=2;
        }
        else
        {
            count++;
        }
    }
    for(int i=0;i<k;i++)
    {
        s[q[i]-1]=s[q[i]-1]^1;
        if((q[i]-1)<k-1 && (q[i]-1)>0)
        {
           if(s[q[i]-1]!=s[q[i]-2] && s[q[i]-1]!=s[q[i]])
           {
               count-=2;
           }
           if(s[q[i]-1]==s[q[i]-2] && s[q[i]-1]==s[q[i]])
           {
               count+=2;
           }
        }
        else
        {
            if((q[i]-1)==0)
                {
                     if(s[q[i]-1]==s[q[i]]) 
                     {
                         count++;
                     }
                     if(s[q[i]-1]!=s[q[i]])
                     {
                         count--;
                     }
                }
                if((q[i]-1)==k-1)
                {
                       if(s[q[i]-1]==s[q[i]-2]) 
                     {
                         count++;
                     }
                     if(s[q[i]-1]!=s[q[i]-2])
                     {
                         count--;
                     }
                }         
        }
        cout<<count<<"\n";
    }
}}
return 0;

}
thnx for trying and helping…!

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

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

define ll long long

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll int t;
cin>>t;
for(ll int i=0;i<t;i++)
{
ll int n,k;
cin>>n>>k;
string s;
cin>>s;
ll int a[k+1]={};
for(ll int x=1;x<=k;x++)
{
cin>>a[x];
}
ll int len=0;
for(ll int r=0;r<s.size()-1;r++)
{
// cout<<s[r]<<s[r+1]<<endl;
if(s[r]==s[r+1])
len+=2;
else
len+=1;
}
if(n==1)
{
cout<<“0”<<endl;
continue;
}
//cout<<len<<endl;
for(ll int j=1;j<=k;j++)
{
if(s[a[j]-1]==‘0’)
{
s[a[j]-1]=‘1’;
//cout<<“z”<<endl;
}
else
{
s[a[j]-1]=‘0’;
}
if(s[a[j]-1]==s[a[j]] && a[j]>1 && a[j]<k)
{
len++;
}
if(s[a[j]-1]!=s[a[j]] && a[j]>1 && a[j]<k)
{
len–;
}
if(s[a[j]-1]==s[a[j]-2] && a[j]>1 && a[j]<k)
{
len++;
}
if(s[a[j]-1]!=s[a[j]-2] && a[j]>1 && a[j]<k)
{
len–;
}
if(a[j]==1)
{
if(s[a[j]-1]==s[a[j]])
{
len++;
}
else
{
len–;
}
}
if(a[j]==k)
{
if(s[a[j]-2]==s[a[j]-1])
len++;
else
len–;
}
cout<<len<<endl;
}
}

return 0;

}
can someone tell me why my code is showing wrong answer @darshancool25

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

https://www.codechef.com/viewsolution/47363078
the link

1 Like
1
6 4
111010
4 5 6 3

thnku vry much …!
I was doing such a silly mistake…!

1 Like

By sample test case do you mean

1
3 3
010
2 1 3`
cause it is giving right output for me in Sublime text

There’s Undefined Behaviour for you :man_shrugging:

Can you please tell me why? I really don’t know. I only use CC IDE or Sublime Text and it’s giving correct output for both for sample test case

Out-of-Bounds accesses are Undefined Behaviour, meaning anything can happen - you might get the right answer; you might get the wrong answer; it might crash. It’s almost impossible to predict what will happen.

This is giving an output of

9
9
8
6
for me

I’m so sorry! It gave AC when I did

if (s[1] != s[0] && n > 1)

I wouldn’t have gone down by a division if I corrected this.
It was failing for the simplest TC,

1 
1 1
1
1
1 Like

Thank you I’ll look into it :slight_smile:

1 Like

Yeah it worked using pass by reference. Thank you!

1 Like

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Code link

Can anyone plzz tell my error…i have used the similar approach…!!

Consider the test input:

1
4 8
0110
1 4 2 1 3 2 1 2