Maximum Subarray Sum Problem


in this code how we are accessing the cumSum[-1] at i=0 in line no 28 and what is the sense of it???

arr[-1] means one step back of arr[0]
because internally arr is treated as pointer
so arr[0] means arr+0 and arr[-1] menas arr-1
hence we go one step back

but now arr-1 address may exits or not

if this memory is given to your program then you can access it
but if it is not given to your program then show run error

try to avoid these things they are uncertain depend on compiler memory allocation

if you want to ask something about this feel free to ask
i hope this helps
if yes show your response with :heart: and don’t forgot to mark doubt as resolved

so could you plz edit the code so that it could not access the -1 position

for this you have to simply put a if condition before that

i am saying that instead of this line
cumSum[i] = cumSum[i-1] + arr[i];
put a if condition
if(i>0) cumSum[i] = cumSum[i-1] + arr[i];
else cumSum[i]=arr[i];