Maximum Subarray Sum 2

at 05:50 mins
sir, why we have to take the first element of the array out side of the loop ?
and do this cumsum[0]=a[0];

cant we just run our loop from 0 itself to <n ?
like this
for(int i=0;i<n;i++)
{
cin>>a[i];
cumsum[i] = cumsum[i - 1] + a[i];
}

hello @heyiamtushar

if we do like this then when i=0 at that time i-1 will be -1, and negative index access in not allowed.

so to avoid it we have handled that case seprately by doing this ->

sir, please check my code. with input 1,2,3,4,5 output is coming 14 https://ide.codingblocks.com/s/465136

check now->

but sir if i am using the input from the video the output is correct, only if i am using 1,2,3,4,5 the output is incorrect.

see u r accessing negative index, and value can be anything at negative value which might affect the result , so its better to avoid negative 8ndex case by handling it explicitly

okay, thank you so much !!

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.