Kadane's Algorithm - Simple
Kadane's Algorithm - Simple is a medium arrays & hashing problem solved with the kadane's algorithm pattern.
The best approach, optimal (kadane's algorithm), runs in O(n) time and O(1) space.
Below are 2 approaches in Java, from brute force up.
Problem
Given an integer array arr, find the largest sum of any non-empty contiguous subarray.
Examples
Example 1
- Input
arr = [2, -4, 3, -1, 2, -5, 4]- Output
4- Why
- The subarray [3, -1, 2] sums to 4.
Example 2
- Input
arr = [-3, -1, -2]- Output
-1- Why
- All values are negative, so the best subarray is the single largest element.
Constraints
1 <= arr.length <= 10^5-10^4 <= arr[i] <= 10^4- The subarray must contain at least one element.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.
| Approach | Time | Space |
|---|---|---|
| Brute force | O(n²) | O(1) |
| Optimal (Kadane's algorithm) | O(n) | O(1) |
1Brute force
O(n²)All n(n + 1) / 2 subarrays are summed incrementally.O(1)Try every start index and extend to every end index, keeping a running sum and the best seen.
- For each i, reset sum = 0.
- For each j from i, add arr[j] and update best.
class Solution {
long maxSubarraySum(int[] arr) {
long best = Long.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
long sum = 0;
for (int j = i; j < arr.length; j++) {
sum += arr[j];
best = Math.max(best, sum);
}
}
return best;
}
}2Optimal (Kadane's algorithm)
O(n)One pass.O(1)Two variables.The best subarray ending at index i either extends the best subarray ending at i - 1 or starts fresh at i. Start fresh whenever the carried sum is negative.
- cur = best = arr[0].
- For each next x: cur = max(x, cur + x).
- best = max(best, cur).
class Solution {
long maxSubarraySum(int[] arr) {
long cur = arr[0], best = arr[0];
for (int i = 1; i < arr.length; i++) {
cur = Math.max(arr[i], cur + arr[i]);
best = Math.max(best, cur);
}
return best;
}
}Edge cases to test
- All negative numbers
- Single element
- The whole array is the best subarray
Hints
Hint 1
If the running sum ending at the previous index is negative, is it worth carrying forward?
FAQ
What is the best time complexity for Kadane's Algorithm - Simple?
Optimal (Kadane's algorithm) runs in O(n) time and O(1) extra space. One pass.
Which pattern does Kadane's Algorithm - Simple use?
It is a arrays & hashing problem that uses the kadane's algorithm pattern. Other problems with the same pattern: Find minimum subarray sum, Kadane's Algo - Circular Array - No Extra Space.
Is there a brute force solution for Kadane's Algorithm - Simple?
Yes. Brute force takes O(n²) time and O(1) space. Try every start index and extend to every end index, keeping a running sum and the best seen.
Which edge cases should I test for Kadane's Algorithm - Simple?
All negative numbers; Single element; The whole array is the best subarray.