What if everything in the array is negative

You take the max_sum as 0. Incase if we input the array -1,-2,-3,-4 then the maximum sub array is -1. But in your case since you gave the max_sum as 0, the statement if(current_sum>max_sum) will never get in. And the output of the code will be 0, instead of minus 1. This is a boundary case and will error out for sure

Yes
Incase if we input the array -1,-2,-3,-4 then
since you gave the max_sum as 0, the statement if(current_sum>max_sum) will never get in. And the output of the code will be 0, instead of minus 1.
And it has to be so. Thus we get the correct answer.
In case of completely negative array, we would consider only null element and not include any element of the array.
Hope you get it.

So you are telling, if the array is negative . Say I give -5 ,-6,-2,-3 . The maximum sum of the sub array is -2 . But as per the code it’s 0 . And it’s the expected behavior ? If the entire array is negative the maximum sum of that sub array would be lowest negative number in the array and not zero right ?

Hey, yes you are right if the entire array is negative the maximum sum of that sub array would be lowest negative number in the array and not zero.

Yes. But in this code it will give zero , that’s what I am trying to clarify :sweat_smile:

Yes, in this code it will give 0 so, it is preferred that you initialize the max variable with arr[0] instead of 0 in starting.