# CHARGES - Editorial

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!

#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!

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

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

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!

``````1