Kadane’s Algorithm

Ajay R Bharadwaj
2 min readApr 6, 2022

Today we will be understanding what Kadane’s Algorithm is and what its problem-solving aspects are that will help us solve the “Maximum Subarray Sum” Problem. In this article, we will be understanding the algorithm and the code for Kadane’s Algorithm with an appropriate example. Finally, we will have a look at the algorithm’s time complexity as well as its real-world applicability. So let’s get this party started!

Understanding Kadane’s Algorithm:

The maximum subarray problem is among the well-known dynamic programming problems out there. But, you must be thinking that the statement appears silly as the answer would be the sum of the elements of the given array. Unfortunately, that is incorrect. The said array will contain negative integer elements, which will reduce the array’s total sum. This is where Kadane’s algorithm steps in.

Now, in this case, the technique will identify the longest subarray in the 1-dimensional integer array with the highest possible total. After comprehending the issue description, everyone’s first response will be to solve the problem by brute force. However, doing so will result in a temporal complexity of O(n²), which is not ideal. As a result, we will use Kadane’s technique, which solves the problem by traversing the entire array and tracking the sum thus far and the maximum total with two variables. The most essential thing to remember while utilizing this technique is the condition that will be used to update both variables.

Let's See The Algorithm:

Dry Run :

Few Examples :

Time Complexity:

Considering just one for loop is to be run throughout the algorithm, the time complexity of kadane’s approach of an array having n integer elements is O(n). Furthermore, the algorithm’s auxiliary space complexity is O. (1).

Application :

There are a few uses for Kadane’s method, some of which are listed below:

  1. Largest Subarray Sum
  2. Business Analysis
  3. Process Method for Images

and many more…….

--

--