Kadane's Algorithm
Kadane's Algorithm
Some of you may think it’ll be a sum of all elements in an array. But what if there will be negative
integer elements in the array, which will decrease the array’s total sum.
Thus, we can see that the sum of all elements in an array isn’t always the case.
A simple idea of Kadane’s algorithm is to look for all positive contiguous segments of the array and
keep track of the maximum sum contiguous subarray among all positive segments.
First, we will consider two elements, one which stores the maximum end of the subarray and
another which stores the maximum sum so far.
Each time we get a positive sum, we compare it with max_so_far and update max_so_far if it is
greater than it.
Start
max_so_far = 0
max_ending_here = 0
if(max_ending_here < 0)
max_ending_here = 0
max_so_far = max_ending_here
5. return max_so_far