CHARGES - Editorial

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

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

Solution: 47346743 | CodeChef 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