Problem in maximum subarray sum 2

It seems like the first element of the array is not taken into account when it comes to calculating the maximum sum of the sub array can you please check out and help me if i am wrong
thank you

Hi,
can u share the code u are referring to

ok… so that first element is being used in computing the ans… but it is not used in

for(int i=1;i<n;i++){
        cin>>a[i];
        cumSum[i]=cumSum[i-1]+a[i];
}

here because we are calculating prefix sum of array… so sum till 0th index would be just the 0th element…
hope it clears ur doubt…

Yeah i get it sir but can’t we expect the exact answer like when i used some test cases it is showing the wrong answer, for example down here i have posted the screen shot of the problem, there in the input i have given the elements as 10 8 9 0 2 3 so i should get an output of 32 but it isn’t the case. so what we have to do to overcome this type of problem sir, thank you for replying me sir hope i will get clarified.

save ur code on ide and send… i will correct it…

https://ide.codingblocks.com/s/623805# here is the code sir
thank you for helping me

Hi @swaroop_stellar
current_sum=cumSum[j]-(i-1 >= 0 ? cumSum[i-1] : 0);
just put this check so that i-1 is not less than 0… it will work fine…
bcoz if i is 0 then i-1 give -1… which causes problem…
corrected code --> https://ide.codingblocks.com/s/624586
i hope it would clear all ur doubts

Thank you so much ji for making this clear to me :relaxed:

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.

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.