Kadane’s Algorithm — (Dynamic Programming) — How and Why does it Work? | by Rohit Singhal


On this Article we offer you detailed Data on Kadane’s Algorithm — (Dynamic Programming) — How and Why does it Work? | by Rohit Singhal

Rohit Singhal

If you might be right here, then chances are high that you just have been attempting to unravel the “Most Subarray Drawback” and got here throughout Kadane’s Algorithm however couldn’t determine how one thing like that’s working. Or possibly you have been uninterested in utilizing Kadane’s Algorithm as a “black-box”. Or possibly you needed to know the dynamic programming facet of it. Or possibly you simply wish to find out about a brand new idea which may make you higher at programming. Regardless of the cause, you’ve come to the precise place.

To raised understand Kadane’s Algorithm, first, we might undergo a brief introduction of Dynamic Programming. Then, we might take a look at a fairly common programming drawback, the Most Subarray Drawback. We might see how this drawback will be solved utilizing a brute pressure method after which we might attempt to enhance our method and provide you with a greater algorithm, aka, Kadane’s Algorithm.

So, let’s get into it.

Dynamic Programming

Dynamic Programming is a technique for fixing a posh drawback by breaking it down into a group of easier subproblems, fixing every of these subproblems simply as soon as, and storing their options utilizing a memory-based knowledge construction (array, map, and so on.). So the following time the identical sub-problem happens, as an alternative of recomputing its resolution, one merely appears up the beforehand computed resolution, thereby saving computation time.

Right here’s a superb rationalization on the idea of Dynamic Programming on Quora — Jonathan Paulson’s reply to How ought to I clarify dynamic programming to a 4-year-old?

Although there’s extra to dynamic programming, we might transfer ahead to know the Most Subarray Drawback.

Most Subarray Drawback

The most subarray drawback is the duty of discovering the biggest attainable sum of a contiguous subarray, inside a given one-dimensional array A[1…n] of numbers.

Most Sum Subarray (In Yellow)

For instance, for the array given above, the contiguous subarray with the biggest sum is [4, -1, 2, 1], with sum 6. We might use this array as our instance for the remainder of this text. Additionally, we might assume this array to be zero-indexed, i.e. -2 can be referred to as because the ‘0th’ factor of the array and so forth. Additionally, A[i] would symbolize the worth at index i.

Now, we might take a look at a really apparent resolution to the given drawback.

Brute Pressure Strategy

One very apparent however not so good resolution is to calculate the sum of each attainable subarray and the utmost of these can be the answer. We will begin from index 0 and calculate the sum of each attainable subarray beginning with the factor A[0], as proven within the determine beneath. Then, we might calculate the sum of each attainable subarray beginning with A[1], A[2] and so forth as much as A[n-1], the place n denotes the dimensions of the array (n = 9 in our case). Observe that each single factor is a subarray itself.

Brute Pressure Strategy: Iteration 0 (left) and Iteration 1 (proper)

We are going to name the utmost sum of subarrays beginning with factor A[i] the local_maximum at index i. Thus after going via all of the indices, we might be left with local_maximum for all of the indices. Lastly, we will discover the utmost of those local_maximums and we might get the ultimate resolution, i.e. the utmost sum attainable. We might name this the global_maximum.

However you would possibly discover that this isn’t an excellent technique as a result of as the dimensions of array will increase, the variety of attainable subarrays will increase quickly, thus rising computational complexity. Or to be extra exact, if the dimensions of the array is n, then the time complexity of this resolution is O(n²) which isn’t excellent.

How can we enhance this? Is there any manner to make use of the idea of dynamic programming? Let’s discover out.

Kadane’s Algorithm

On this part, we might use the brute pressure method mentioned above once more, however this time we might begin backward. How would that assist? Let’s see.

We might begin from the final factor and calculate the sum of each attainable subarray ending with the factor A[n-1], as proven within the determine beneath. Then, we might calculate the sum of each attainable subarray ending with A[n-2], A[n-3] and so forth as much as A[0].

Backward Brute Pressure Strategy: Iteration 0 (left) and Iteration 1 (proper)

Now let’s deal with the subarrays ending with the factor A[4] (=-1) and A[5] (=2) proven within the determine beneath.

From the determine above, we see that the local_maximum[4] is the same as 3 which is the sum of the subarray [4, -1]. Now take a look on the subarrays ending with A[5]. You’ll discover that these subarrays will be divided into two components, the subarrays ending with A[4] (highlighted with yellow) and the one factor subarray A[5] (in inexperienced).

Let’s say one way or the other I do know the local_maximum[4]. Then we see that to calculate the local_maximum[5], we don’t have to compute the sum of all subarrays ending with A[5] since we already know the outcome from arrays ending with A[4]. Observe that if array [4, -1] had the utmost sum, then we solely have to test the arrays highlighted with the crimson arrows to calculate local_maximum[5]. And this leads us to the precept on which Kadane’s Algorithm works.

This manner, at each index i, the issue boils all the way down to discovering the utmost of simply two numbers, A[i] and (A[i] + local_maximum[i-1]). Thus the utmost subarray drawback will be solved by fixing these sub-problems of discovering local_maximums recursively. Additionally, be aware that local_maximum[0] can be A[0] itself.

Utilizing the above technique, we have to iterate via the array simply as soon as, which is rather a lot higher than our earlier brute pressure method. Or to be extra exact, the time complexity of Kadane’s Algorithm is O(n).

Lastly, let’s see how this all would work in code.

Code Walkthrough

Beneath is a really a lot self-explanatory implementation (in C++) of a perform which takes an array as an argument and returns the sum of the utmost subarray.

Observe that as an alternative of utilizing an array to retailer local_maximums, we’re merely storing the newest local_maximum in an int kind variable ‘local_max’ as a result of that’s what we have to calculate subsequent local_maximum. Additionally, as we’re utilizing a variable ‘global_max’ to maintain observe of the utmost worth of local_maximum, which ultimately comes out to be the required output.


Due to the best way this algorithm makes use of optimum substructures (the utmost subarray ending at every place is calculated in a easy manner from a associated however smaller and overlapping subproblem: the utmost subarray ending on the earlier place) this algorithm will be considered as a easy instance of dynamic programming. Kadane’s algorithm is ready to discover the utmost sum of a contiguous subarray in an array with a runtime of O(n).

#Kadanes #Algorithm #Dynamic #Programming #Work #Rohit #Singhal