CHARGES - Editorial

Can someone explain why there is a Time Limit Exceeded error here? View Solution
@daanish_adm @cherry0697 @monogon

@henrychen222
Thanks a lot sir . I have one doubt .
if string in c++ is immutable objects the how can
s[index] ^= 1;
this line is valid ?

One thing that’s bad for performance: IsSame and Score both take strings by value instead of by reference, causing the passed string to be copied each time they are called.

Yaa, I realized later that I didn’t consider the edge case of string of 1 unit length. Thanks for pointing it out.

although this code passes all the test cases still gives WA please help…
/*
first of all as stated in the question calculate the distance between the first and last
particle. denote the change in the given position by the variable “change”
so to check for the change in the string we just haveto check in the vicinity that is left and the right
THIS PROBLEM IS MEDIUM FOR ME
so after making a change if the particle in vicinity is opposite then len–
and if they are of the same chare then len++

/
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll t;
cin>>t;
while(t–)
{
ll n,k;
cin>>n>>k;
string s;
cin>>s;//since we only need a single string therefore not creating a loop
ll length=n-1;//distance between first and last
//calculating the initial length
for(ll i=1;i<n;i++)
{
length+=s[i]==s[i-1];//
**
}
while(k–)//executing till changes become zero
{
ll queries;
cin>>queries;
queries–;
if(s[queries]==‘1’)
{
s[queries]=‘0’;
}
else
{
s[queries]=‘1’;
}
if(queries<n-1) //charge is flipped and now checking vicinity
{
if(s[queries]==s[queries+1])
{
length++;
}
else
{
length–;
}
}
// now checking if the previous configuration was same or different
if(queries>0)
{
if(s[queries]==s[queries-1])
{
length++;
}
else
{
length–;
}
}
cout<<length<<endl;
//woah!! this was a nasty one…xd

}
return 0;
    }

}

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

Edit:

Deciphered it. Obvious one:

2
3 3
010
2 1 3
3 3
010
2 1 3

This is something people should try whenever they have a WA.

Edit2:

Second one of these today, interestingly :slight_smile: Running testcases with array - #2 by ssjgz

strings in C++ are not immutable.

1 Like

Your solution fails for the following test input:

1
1 1
0
1

@sarcastic_yogi Yours too :slight_smile:

@aadarshsinha Yours has an out-of-bounds access with this testcase.

1 Like

s[index] ^= 1, means index char switch. xor 1 change ‘0’ → ‘1’, ‘1’ → ‘0’ easily, I think c++ string chars can be changed.

example s= ‘abc’, s[1] = ‘d’. the s = ‘adc’

1 Like

@ssjgz
Understood sir.

1 Like

@henrychen222
thanks sir , now i understand.

#include
using namespace std;

int main() {
int x,a,b,c,d,y,z;
string s;
cin >> x;
for(int i=0;i<x;i++)
{
cin>>a>>b;
char arr[a-1];
char k;
int jar[a-1];
cin>>s;
for(int j=0;j<a;j++){
arr[j]=s[j];
k=arr[j];
jar[j] = (int)k;
}
for(int lo=0;lo<b;lo++){
cin>>d;
if(jar[d-1]==0)
{
jar[d-1]=1;
}
else
{
jar[d-1]=0;
}
int dis=0;
for(int ll=1;ll<a;ll++){
if(jar[ll]-jar[ll-1]==0){
dis=dis+1;
}
else{
dis=dis+2;
}
}
cout<<dis<<endl;
}
}

return 0;

}
@darshancool25 can you tell me what is the problem if I code it this way
and why the submission has errors
It will be very kind of you.Thank you.

This has out-of-bounds accesses with the sample test input:

[simon@simon-laptop][08:42:22]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling makasam-CHARGES.cpp
+ g++ -std=c++14 makasam-CHARGES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
makasam-CHARGES.cpp: In function ‘int main()’:
makasam-CHARGES.cpp:5:15: warning: unused variable ‘c’ [-Wunused-variable]
     int x,a,b,c,d,y,z;
               ^
makasam-CHARGES.cpp:5:19: warning: unused variable ‘y’ [-Wunused-variable]
     int x,a,b,c,d,y,z;
                   ^
makasam-CHARGES.cpp:5:21: warning: unused variable ‘z’ [-Wunused-variable]
     int x,a,b,c,d,y,z;
                     ^
+ set +x
Successful
[simon@simon-laptop][08:42:28]
[~/devel/hackerrank/otherpeoples]>echo "1
3 3
010
2 1 3" | ./a.out
makasam-CHARGES.cpp:16:18: runtime error: index 2 out of bounds for type 'char [*]'
makasam-CHARGES.cpp:17:20: runtime error: index 2 out of bounds for type 'char [*]'
makasam-CHARGES.cpp:18:18: runtime error: index 2 out of bounds for type 'int [*]'
makasam-CHARGES.cpp:32:26: runtime error: index 2 out of bounds for type 'int [*]'
4
3
makasam-CHARGES.cpp:22:23: runtime error: index 2 out of bounds for type 'int [*]'
makasam-CHARGES.cpp:28:24: runtime error: index 2 out of bounds for type 'int [*]'
2
1 Like

Everything ssjgz has written is true. I would just like to add few points :

  1. If you ever need to convert a single character (my_char) to its corresponding int value, heres an example : int int_val = my_char - '0';
    Note that type casting int_val = (int)(my_char) wont work !!
  2. In your code after each of the k changes, you are recalculating the answer from scratch again (which is quite unnecessary and slow). I suggest have a look at the video editorial for understanding a better approach. :slight_smile:
1 Like

Woah thanks this corner case just didn’t come to my mind :sweat_smile:, how do you find such corner cases?
Edit: I changed the case when n==1 to output it 0 and continue then too it is giving WA

1 Like

The usual way - simple brute-force solution plus randomised testcase generator :slight_smile:

Please link to your most recent WA solution.

1 Like

https://www.codechef.com/viewsolution/47333753

That doesn’t pass the sample test input :slight_smile: Pay attention to compiler warnings!

[simon@simon-laptop][10:43:08]
[~/tmp/DONTSYNC/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling mickey_03-CHARGES.cpp
+ g++ -std=c++14 mickey_03-CHARGES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
mickey_03-CHARGES.cpp: In function ‘int main()’:
mickey_03-CHARGES.cpp:38:13: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
             if(n==1) cout<<"0\n"; continue;
             ^~
mickey_03-CHARGES.cpp:38:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
             if(n==1) cout<<"0\n"; continue;
                                   ^~~~~~~~
+ set +x
Successful
[simon@simon-laptop][10:43:18]
[~/tmp/DONTSYNC/hackerrank/otherpeoples]>echo "1                 
3 3
010
2 1 3" | ./a.out

2 Likes

Very Simple Code: Link

Can you please🙏 tell me which test case am i missing . I have tried all including n=1 ,but still WA
link : Solution: 47336440 | CodeChef