Will it work if all element of array are negative?
Kadane's algorithm for maximum sum subarray
won’t it give 0 as the sum instead of giving a -ve value as maximum sum
the algorithm is not what u are thinking off(I guess)…
Kadane Algo…
Case 1.
go through the array once, and if there are only negative numbers, output the maximum of the array, in one pass.
Case 2. (if there exist some non negative numbers)
then, use your code as stated above
This is complete algorithm…
Hit a like button if this is clear!!
no you dont hve to divide it in two cases
look at the function above when all elements are negative your max_ending_here will always equal to the current value and hence max_so_far will only get compared with single values and will automatically give max value in case of all negatives.
why don’t you take simple array of negative elements and run for this algo you will get it.