Kadane's algorithm (max sum)

what if the array has only negative numbers

max sum of the array will be zero

Hey Neeraj, according to Kadane’s algorithm it will be zero but to handle this case you can use a for loop to check if all the elements of the array are -ve, and if you find that all the elements of the array are -ve then you can return the maximum no. of array.

you mean to say that if n=5
The elements are
-1
-2
-3
-4
-5
so ans will 5
in this senario

No, answer should be -1.

Kadane’s algorithm is used only when atleast one number in the array is positive.
To handle the case for all negative numbers you need to provide an extra condition.
While getting the input array check if any positive number exists or not.
if No positive number exists find the maximum number.
else use kadane’s algorithm.

Hey Vinod, i guess i have told the same thing.