When we have to maximum subarray sum ,if we have array 1 2 3 4 5, i am getting maxsum=14 and maximum subarray as 2 3 4 5. Please tell how to get correct output

This happens as when we are finding current sum at i=0, currentsum= cumsum[j]-cumsum[i-1] thus i-1 gives ‘’-1 " .

hi Ujjwal,
image

just make this change… as if i-1 is less than 0 then it tries to search for negative index and thus give wrong ans… so subtract 0 in that case…

corrected code --> https://ide.codingblocks.com/s/642404

1 Like

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.