CHARGES - Editorial

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 : CodeChef: Practical coding for everyone

Already told you :slight_smile:

Fix the out-of-bounds access.

1 Like

Ohh yess I tried to do it in one line but should have put in curly braces, :100: thanks for your help got AC!
But I didā€™nt get any compiler warnings as such :confused: when I executed thatā€¦

1 Like

I just removed the void solve() and placed all the code inside main .Now i am getting correct answer . Can you please tell me why is it so? what wrong in using another function ?

Your AC solution still has the out-of-bounds access:

[simon@simon-laptop][14:59:27]
[~/devel/hackerrank/otherpeoples]>./codechef-download-solution.rb https://www.codechef.com/viewsolution/47337805
Solution written to: aadarshsinha-CHARGES.cpp
[simon@simon-laptop][14:59:37]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh                                                       
Compiling aadarshsinha-CHARGES.cpp
+ g++ -std=c++14 aadarshsinha-CHARGES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][14:59:40]
[~/devel/hackerrank/otherpeoples]>echo "1                                                                       
1 1
0
1" | ./a.out
aadarshsinha-CHARGES.cpp:38:42: runtime error: index 1 out of bounds for type 'char [*]'
aadarshsinha-CHARGES.cpp:39:47: runtime error: index 1 out of bounds for type 'char [*]'
0

so you got lucky.

1 Like

But i am still confused , when i used void solve() function and write the code there it give me wrong answer and when i write the entire code inside main function it gives my correct answer .Why is it so ?
using void solve() : Solution: 47336440 | CodeChef
without viod solve() : Solution: 47341451 | CodeChef

Thereā€™s Undefined Behaviour for you :man_shrugging:

1 Like

okay , thank you so much for helping me out .

1 Like

CodeChef: Practical coding for everyone pls help with why this is WAing

Out of bounds access on sample testcase:

[simon@simon-laptop][18:19:24]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling arjav_jain-CHARGES.cpp
+ g++ -std=c++14 arjav_jain-CHARGES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][18:19:28]
[~/devel/hackerrank/otherpeoples]>echo "1
3 3
010
2 1 3
" | ./a.out
/usr/include/c++/7/bits/basic_string.h:1057: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]: Assertion '__pos <= size()' failed.
Aborted (core dumped)

Can someone please tell what is wrong with my code?
https://www.codechef.com/viewsolution/47349723
I am getting RE (SIGSEGV) error

Exactly the same mistake as this guy: CHARGES - Editorial - #77 by ssjgz

@ssjgz Sir can you please review this code. I am getting TLE at the output.
Solution Link

Out-of-bounds access on the following test input:

1
4 8
0110
1 4 2 1 3 2 1 2
[simon@simon-laptop][20:32:25]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling ikshul130-CHARGES.cpp
+ g++ -std=c++14 ikshul130-CHARGES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][20:32:29]
[~/devel/hackerrank/otherpeoples]>echo "1
4 8
0110
1 4 2 1 3 2 1 2
"  | ./a.out
5
6
4
5
/usr/include/c++/7/bits/basic_string.h:1057: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]: Assertion '__pos <= size()' failed.
Aborted (core dumped)

See point 2. of this post.

Can you please suggest what changes do I need to make in my code? and also, how can I think of it on my own?

@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ā€¦!