Kadane's Algorithm Explained:
Kadane’s Algorithm is a famous method used to solve the Maximum Subarray Sum problem efficiently. The goal is to find the contiguous subarray in a given array that has the largest sum.
How It Works:
Iterate Through the Array: Traverse the array, considering each element as part of a subarray.
Keep Track of Two Variables:
currentSum
: The sum of the current subarray being evaluated.maxSum
: The maximum sum encountered so far.
Decision at Each Step: For each element, decide:
Should the current element be added to the existing subarray (
currentSum + arr[i]
)?Or should a new subarray start from this element (
arr[i]
)?Take the maximum of these two options.
Update Maximum Sum: If the
currentSum
exceedsmaxSum
, updatemaxSum
.End Result: At the end of the iteration,
maxSum
contains the largest sum of a contiguous subarray.
Algorithm:
Initialize:
currentSum = 0
maxSum = Integer.MIN_VALUE
(or a very small value to handle negative arrays).
Iterate through the array:
Update
currentSum = max(arr[i], currentSum + arr[i])
.Update
maxSum = max(maxSum, currentSum)
.
Return
maxSum
.
Example Walkthrough:
Input:
arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Steps:
Index | Current Element | currentSum Calculation | maxSum Update |
0 | -2 | currentSum = max(-2, 0 + (-2)) = -2 | maxSum = max(-∞, -2) = -2 |
1 | 1 | currentSum = max(1, -2 + 1) = 1 | maxSum = max(-2, 1) = 1 |
2 | -3 | currentSum = max(-3, 1 + (-3)) = -2 | maxSum = max(1, -2) = 1 |
3 | 4 | currentSum = max(4, -2 + 4) = 4 | maxSum = max(1, 4) = 4 |
4 | -1 | currentSum = max(-1, 4 + (-1)) = 3 | maxSum = max(4, 3) = 4 |
5 | 2 | currentSum = max(2, 3 + 2) = 5 | maxSum = max(4, 5) = 5 |
6 | 1 | currentSum = max(1, 5 + 1) = 6 | maxSum = max(5, 6) = 6 |
7 | -5 | currentSum = max(-5, 6 + (-5)) = 1 | maxSum = max(6, 1) = 6 |
8 | 4 | currentSum = max(4, 1 + 4) = 5 | maxSum = max(6, 5) = 6 |
Output:
maxSum = 6
Advantages:
Time Complexity:
- O(n): Only one traversal of the array is required.
Space Complexity:
- O(1): No additional data structures are used, just two variables.
Edge Cases:
All Negative Numbers:
Input:
arr = [-3, -5, -1, -2]
Output:
-1
(The maximum subarray is the single largest element,-1
).
Single Element Array:
Input:
arr = [5]
Output:
5
.
All Positive Numbers:
Input:
arr = [1, 2, 3, 4]
Output:
10
(The entire array is the subarray).
Code Example in Java:
public class KadaneAlgorithm {
public static int maxSubArraySum(int[] arr) {
int currentSum = 0;
int maxSum = Integer.MIN_VALUE;
for (int num : arr) {
currentSum = Math.max(num, currentSum + num);
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}
public static void main(String[] args) {
int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
System.out.println("Maximum Subarray Sum: " + maxSubArraySum(arr));
}
}
Real-Life Applications:
Financial Analysis:
- Identify the period with the highest profit in stock prices.
Signal Processing:
- Detect the strongest signal in a noisy dataset.
Gaming:
- Find the maximum score achievable in a segment of a game.