That doesn’t pass the sample test input 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
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
Fix the out-of-bounds access.
Ohh yess I tried to do it in one line but should have put in curly braces, thanks for your help got AC!
But I did’nt get any compiler warnings as such when I executed that…
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.
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
okay , thank you so much for helping me out .
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
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)
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?