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
Everything ssjgz has written is true. I would just like to add few points :
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 !!
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.
Woah thanks this corner case just didn’t come to my mind , 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
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
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
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