Javatpoint Logo
Javatpoint Logo

Kadane's Algorithm

Kadane's algorithm is a dynamic programming approach used to solve the maximum subarray problem, which involves finding the contiguous subarray with the maximum sum in an array of numbers. The algorithm was proposed by Jay Kadane in 1984 and has a time complexity of O(n).

History of Kadane's algorithm:

Kadane's algorithm is named after its inventor, Jay Kadane, a computer science professor at Carnegie Mellon University. He first described the algorithm in a paper titled "Maximum Sum Subarray Problem" published in the Journal of the Association for Computing Machinery (ACM) in 1984.

The problem of finding the maximum subarray has been studied by computer scientists since the 1970s. It is a well-known problem in the field of algorithm design and analysis and has applications in a wide range of areas, including signal processing, finance, and bioinformatics.

Prior to Kadane's algorithm, other algorithms had been proposed for solving the maximum subarray problem, such as the brute-force approach that checks all possible subarrays and the divide-and-conquer algorithm. However, these algorithms have higher time complexities and are less efficient than Kadane's algorithm.

Kadane's algorithm is widely used in computer science and has become a classic example of dynamic programming. Its simplicity, efficiency, and elegance have made it a popular solution to the maximum subarray problem and a valuable tool in algorithm design and analysis.

Working of Kadene's Algorithm:

The algorithm works by iterating over the array and keeping track of the maximum sum of the subarray ending at each position. At each position i, we have two options: either add the element at position i to the current maximum subarray or start a new subarray at position i. The maximum of these two options is the maximum subarray ending at position i.

We maintain two variables, max_so_far and max_ending_here, to keep track of the maximum sum seen so far and the maximum sum ending at the current position, respectively. The algorithm starts by setting both variables to the first element of the array. Then, we iterate over the array from the second element to the end.

At each position i, we update max_ending_here by taking the maximum of the current element and the current element added to the previous maximum subarray. We then update max_so_far to be the maximum of max_so_far and max_ending_here.

The algorithm returns max_so_far, which is the maximum sum of any subarray in the array.

Here's the step-by-step process of Kadane's Algorithm:

1. Initialize two variables, max_so_far and max_ending_here, to the first element of the array.

max_so_far = arr[0]

max_ending_here = arr[0]

2. Iterate over the array from the second element to the end:

for i from 1 to n-1 do:

3. Calculate the maximum sum ending at the current position:

max_ending_here = max(arr[i], max_ending_here + arr[i])

4. Update max_so_far to be the maximum of max_so_far and max_ending_here:

max_so_far = max(max_so_far, max_ending_here)

5. Return max_so_far as the maximum sum of any subarray in the array.

The time complexity of Kadane's Algorithm is O(n), where n is the length of the input array. This makes it a very efficient solution to the maximum subarray problem.

Example:

Let's see at an example of how Kadane's algorithm works:

Suppose we have the following array of integers:

We want to find the maximum subarray sum of this array. We can apply Kadane's algorithm to solve this problem.

We start by initializing two variables:

  • max_so_far: This variable will keep track of the maximum subarray sum we have seen so far.
  • max_ending_here: This variable will keep track of the maximum sum ending at the current index.

Then, we iterate through the array, starting from the second element:

Update the current sum by adding the current element to the previous sum:

Update the maximum sum seen so far:

At each iteration, we update the current sum by either adding the current element to the previous sum or starting a new subarray at the current element. We then update the maximum sum seen so far by comparing it with the current sum.

After iterating through the entire array, the value of max_so_far will be the maximum subarray sum of the given array.

In this example, the maximum subarray sum is 6, which corresponds to the subarray [4, -1, 2, 1].

Code Implementation in Java:

Output

Enter the size of the array : 9
Enter the elements of the array : 
-2 1 -3 4 -1 2 1 -5 4
The Maximum contiguous sum in the array is : 6

Code Implementation in C++:

Output

Maximum contiguous sum in the array is : 7

Advantages and Disadvantages of Kadane's algorithm:

Advantages of Kadane's Algorithm:

  • Efficiency: Kadane's Algorithm has a time complexity of O(n), which makes it very efficient for solving the maximum subarray problem. This makes it a great solution for large datasets.
  • Simplicity: Kadane's Algorithm is relatively easy to understand and implement compared to other algorithms for solving the maximum subarray problem, such as the divide-and-conquer algorithm.
  • Space Complexity: Kadane's Algorithm has a space complexity of O(1), which means it uses a constant amount of memory irrespective of the size of the input array.
  • Dynamic Programming: Kadane's Algorithm is a classic example of dynamic programming, a technique that breaks down a problem into smaller subproblems and stores the solutions to these subproblems to avoid redundant computation.

Disadvantages of Kadane's Algorithm:

  • Only finds sum and not the subarray itself: Kadane's Algorithm only finds the maximum sum of the subarray and not the actual subarray itself. If you need to find the subarray that has the maximum sum, you will need to modify the algorithm accordingly.
  • Does not handle negative numbers well: If an input array has only negative numbers, the algorithm will return the maximum negative number instead of 0. This can be overcome by adding an additional step to the algorithm to check if the array has only negative numbers.
  • Not suitable for non-contiguous subarrays: Kadane's Algorithm is specifically designed for contiguous subarrays and may not be suitable for solving problems that involve non-contiguous subarrays.

Applications of Kadane's algorithm:

There are some of its applications like the following:

  • Maximum subarray sum: As we saw in the example above, Kadane's algorithm is used to find the maximum subarray sum of an array of integers. This is a common problem in computer science and has applications in data analysis, financial modeling, and other fields.
  • Stock trading: Kadane's algorithm can be used to find the maximum profit that can be made by buying and selling a stock on a given day. The input to the algorithm is an array of stock prices, and the output is the maximum profit that can be made by buying and selling the stock at different times.
  • Image processing: Kadane's algorithm can be used in image processing applications to find the largest contiguous area of pixels that meet a certain condition, such as having a certain color or brightness. This can be useful for tasks such as object recognition and segmentation.
  • DNA sequencing: Kadane's algorithm can be used in bioinformatics to find the longest subsequence of DNA that meets certain conditions. For example, it can be used to find the longest common subsequence between two DNA sequences or to find the longest subsequence that does not contain certain patterns.
  • Machine learning: Kadane's algorithm can be used in some machine learning applications, such as reinforcement learning and dynamic programming, to find the optimal policy or action sequence that maximizes a reward function.

Therefore, we can say the advantages of Kadane's Algorithm make it a great solution for solving the maximum subarray problem, especially for large datasets. However, its limitations must be considered when using it for specific applications.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA