In the line 35;
currentSum=cumSum[j]- cum[i-1];
initially the loop starts with i=0; then cum[i-1] is cum[-1] and its not possible
Please help me resolve this query why
Hello @wadhwasaab,
You have pointed out a very interesting concept of C++ i.e. undefined Behavior (or unpredictable Behavior).
If you would try to access arr[-1] in java, you would get an “Index out of bound error”.
In python, it will print the element present at last index.
Now coming to c++ and c, the result is unpredictable, but surely not an error.
The value it will return will vary from compiler to compiler.
In this case, it is returning 0.
What is happening exactly?
as you array is a sequence of memory locations. right?
There must be some memory location assigned to arr[0], say 10123
now, arr[-1] points to the memory location before arr[0] in the computer’s memory.
as arr is of type integer so, arr[-1] will point to 10123 - 4=10119
Thus, it returned the value stored at memory location 10119
Why is it happening?
This is because there is no specification for this case in the documentation of c++ language.
Thus, it does not return an error.
You can run the following for better understanding:
Hope, this would help.
Give a like, if you are satisfied.