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.
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
6 4
111010
4 5 6 3
thnku vry much ā¦!
I was doing such a silly mistakeā¦!
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
Thank you Iāll look into it
Yeah it worked using pass by reference. Thank you!
Please either format your code or (better!) link to your submission - the forum software has mangled it and it wonāt compile!
Consider the test input:
1
4 8
0110
1 4 2 1 3 2 1 2
I think the case where n==1 is a pretty misleading one and should have been clearly mentioned in the question itself. My output was set to print ākā times as it should be but if n==1, print should be ā0ā once and NOT ākā times. I only understood this after watching the discussions. This was not mentioned in the video editorial even.