Java Array Program · A25
Maximum Subarray Sum (Kadane's Algorithm) in Java
At each position, either extend the best subarray that ends just before it, or start fresh. Keep the best value seen anywhere.
Problem Statement
Given a non-empty int array, return the largest sum of a contiguous, non-empty subarray.
If every value is negative, the answer is the largest single value, not 0.
Input/Output Examples
[3, -4, 5, -1, 2, -6, 4]Output6Explanation: 5 + (-1) + 2 = 6.[-3, -1, -2]Output-1Explanation: the best subarray is the single element -1.[2, 3]Output5Explanation: the whole array.Visual Explanation
current = max(value, current + value) drops any prefix whose sum has become negative, because such a prefix can only make later sums smaller.
Approaches
Both approaches are linear. Kadane's algorithm tracks the best sum ending at each position; the prefix-sum version subtracts the smallest earlier prefix.
Approach 1: Kadane's Algorithm (Recommended)
Idea: keep the best sum of a subarray ending at the current index, and the best sum overall.
- Start both
currentandbestatarr[0]. - For each next value, set
current = max(value, current + value). - Update
best = max(best, current).
Time: O(n) · Space: O(1).
Approach 2: Prefix Sum Minus Minimum Prefix
Idea: the sum of arr[i..j] is prefix(j) - prefix(i - 1); subtract the smallest earlier prefix.
- Keep a running prefix sum and the smallest prefix seen before the current index (starting at 0).
- At each index, update
bestwithprefix - minPrefix. - Then update
minPrefix.
Time: O(n) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Kadane's Algorithm (Recommended)
Starting from arr[0] instead of 0 is what makes all-negative arrays work.
import java.util.Arrays;
public class KadaneMax {
// best ending here = max(this element alone, previous best ending here + this element)
static int maxSubarraySum(int[] arr) {
int current = arr[0];
int best = arr[0];
for (int i = 1; i < arr.length; i++) {
current = Math.max(arr[i], current + arr[i]);
best = Math.max(best, current);
}
return best;
}
public static void main(String[] args) {
int[][] inputs = {{3, -4, 5, -1, 2, -6, 4}, {-3, -1, -2}, {2, 3}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + maxSubarraySum(arr));
}
}
}
[3, -4, 5, -1, 2, -6, 4] -> 6 [-3, -1, -2] -> -1 [2, 3] -> 5
Time complexity: O(n) · Space complexity: O(1).
Approach 2: Prefix Sum Minus Minimum Prefix
A different view of the same problem that connects it to the stock-profit problem.
import java.util.Arrays;
public class MaxSubarrayPrefix {
// sum(i..j) = prefix(j) - prefix(i - 1); subtract the smallest earlier prefix
static int maxSubarraySum(int[] arr) {
int prefix = 0;
int minPrefix = 0; // the empty prefix before index 0
int best = Integer.MIN_VALUE;
for (int n : arr) {
prefix += n;
best = Math.max(best, prefix - minPrefix);
minPrefix = Math.min(minPrefix, prefix);
}
return best;
}
public static void main(String[] args) {
int[][] inputs = {{3, -4, 5, -1, 2, -6, 4}, {-3, -1, -2}, {2, 3}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + maxSubarraySum(arr));
}
}
}
[3, -4, 5, -1, 2, -6, 4] -> 6 [-3, -1, -2] -> -1 [2, 3] -> 5
Time complexity: O(n) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Kadane's Algorithm | O(n) | O(1) |
| Prefix Sum Minus Minimum Prefix | O(n) | O(1) |
Here n is the number of elements. Both approaches read each value once with a few variables; checking every subarray would take O(n²).
Interview Tips and Recommended Approach
- Handle all-negative input. Initialising
bestto 0 returns a wrong answer for[-3, -1, -2]. - Track the indexes if asked. Record the start whenever a fresh subarray begins.
- Know the circular variant. It combines Kadane's maximum with the total minus the minimum subarray.
- Explain why it is dynamic programming. Each
currentvalue depends only on the previous one.
Recommended Approach: Kadane's Algorithm
Why: it is the classic linear-time solution, uses two variables and handles every input, including all-negative arrays.