CHARGES - Editorial

#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

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. :cry:

All of the Editorialist, Tester and Setter solutions give:

0
0
0
0
0
0
0
0
0

for the test input:

1
1 9
1
1 1 1 1 1 1 1 1 1

If printing out a single 0 gets AC, I guess the testcases must be pretty weak.

1 Like

Can someone please explain why this code is giving Runtime Error?It’s working fine for the given TC,but after submitting it’s giving Runtime Error RE (SIGSEGV).
Thank you!

Solution

[simon@simon-laptop][19:08:05]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling cookieg13-CHARGES.cpp
+ g++ -std=c++14 cookieg13-CHARGES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
cookieg13-CHARGES.cpp: In function ‘int main()’:
cookieg13-CHARGES.cpp:25:17: warning: unused variable ‘i’ [-Wunused-variable]
         int N,K,i,TotalDist=0;
                 ^
+ set +x
Successful
[simon@simon-laptop][19:08:14]
[~/devel/hackerrank/otherpeoples]>echo "1
3 3
010
2 1 3" | ./a.out
cookieg13-CHARGES.cpp:38:23: runtime error: index 2 out of bounds for type 'int [*]'
cookieg13-CHARGES.cpp:42:32: runtime error: index 2 out of bounds for type 'int [*]'
4
3
2