You are viewing a single comment's thread. Return to all comments →
It's pretty obvious you have an ArrayIndexOutOfBoundsException even without trying
If it makes you feel better I tried it, and it has array index out of bound exception. Just revisit it and you'll see. And once again it's clear even without trying. You start at i=0 and you are trying to access item at i-1 which is index -1
You have an array index out of bounds in u r code at line 24 because of negative index. Also, add a condition that if length is 1 display YES.
for( int i = 0 ; i < n ; i++)
lc += a[i-1];
Cheers. Good Day.
I can see that the code passes the test cases, but you still have Undefined Behavior for accessing an out of bounds index. a[-1] where a is an array is always undefined behavior. It means anything can happen, including your program appearing to behave correctly.
Yes, the code is working without a crash because there is a statment to break the loop if condition match.
No it works probably because the 4 bytes before is accessible and contains 0.
This is a classical undefined behavior case and a case of « it works on my computer syndroma ».
Running compiling/running this code with a different compiler (or on a different arch) may break. Just test it with address sanitizer (-fsanitize=address in gcc/clang) or a similar tool …