Negative indexing in cpp

in starting of the loop here it is csum=csum[j] - csum[i-1];
where starts with 0. therefore at start it is csum= csum[0]-csum[-1]; . so what is the value at the -1th index and is it valid in c++.

Here value of csum[-1] will be considered as 0. C++ compiler will assign the value at negative index as 0. You can check that in the ide.
You should prevent negative indexes in the array as it may have some unexpected results depending on the compiler.Here it is not causing any error and taking cumSum[-1] as 0. It basically depends on the compiler.

1 Like

thanks @ pratyush63 , yes as it totally depends upon the compiler then for different ide
how to write the code according to the logic for cumulative sum…how to get the 1st element from the csum array without using negative index?

The -1 index should not be acessed, its a small mistake in the video. I will correct it.

1 Like

Ok bhaia…thanks for assistance

i hope you understand

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like