Java Program to Find Maximum Subarray Sum
currentSum = Math.max(arr[i], currentSum + arr[i]) and maxSum = Math.max(maxSum, currentSum) to find the maximum subarray sum.Examples
How to Think About It
Algorithm
Code
public class MaxSubarraySum { public static int maxSubArray(int[] nums) { int currentSum = nums[0]; int maxSum = nums[0]; for (int i = 1; i < nums.length; i++) { currentSum = Math.max(nums[i], currentSum + nums[i]); maxSum = Math.max(maxSum, currentSum); } return maxSum; } public static void main(String[] args) { int[] arr = {1, -2, 3, 4, -1, 2, 1, -5, 4}; System.out.println(maxSubArray(arr)); } }
Dry Run
Let's trace the array [1, -2, 3, 4, -1, 2, 1, -5, 4] through the code
Initialize
currentSum = 1, maxSum = 1
Index 1 (-2)
currentSum = max(-2, 1 + (-2)) = max(-2, -1) = -1; maxSum = max(1, -1) = 1
Index 2 (3)
currentSum = max(3, -1 + 3) = max(3, 2) = 3; maxSum = max(1, 3) = 3
Index 3 (4)
currentSum = max(4, 3 + 4) = max(4, 7) = 7; maxSum = max(3, 7) = 7
Index 4 (-1)
currentSum = max(-1, 7 + (-1)) = max(-1, 6) = 6; maxSum = max(7, 6) = 7
Index 5 (2)
currentSum = max(2, 6 + 2) = max(2, 8) = 8; maxSum = max(7, 8) = 8
Index 6 (1)
currentSum = max(1, 8 + 1) = max(1, 9) = 9; maxSum = max(8, 9) = 9
Index 7 (-5)
currentSum = max(-5, 9 + (-5)) = max(-5, 4) = 4; maxSum = max(9, 4) = 9
Index 8 (4)
currentSum = max(4, 4 + 4) = max(4, 8) = 8; maxSum = max(9, 8) = 9
| Index | Element | currentSum | maxSum |
|---|---|---|---|
| 0 | 1 | 1 | 1 |
| 1 | -2 | -1 | 1 |
| 2 | 3 | 3 | 3 |
| 3 | 4 | 7 | 7 |
| 4 | -1 | 6 | 7 |
| 5 | 2 | 8 | 8 |
| 6 | 1 | 9 | 9 |
| 7 | -5 | 4 | 9 |
| 8 | 4 | 8 | 9 |
Why This Works
Step 1: Track current sum
We keep a running sum of the current subarray using currentSum. If adding the next element makes the sum smaller than the element alone, we start fresh from that element.
Step 2: Update maximum sum
At each step, we compare currentSum with maxSum and update maxSum if currentSum is larger, ensuring we remember the best sum found.
Step 3: Return result
After checking all elements, maxSum holds the largest sum of any continuous subarray, which we return.
Alternative Approaches
public static int maxSubArrayBruteForce(int[] nums) { int maxSum = Integer.MIN_VALUE; for (int i = 0; i < nums.length; i++) { int currentSum = 0; for (int j = i; j < nums.length; j++) { currentSum += nums[j]; if (currentSum > maxSum) maxSum = currentSum; } } return maxSum; }
public static int maxSubArrayDivideConquer(int[] nums, int left, int right) { if (left == right) return nums[left]; int mid = (left + right) / 2; int leftMax = maxSubArrayDivideConquer(nums, left, mid); int rightMax = maxSubArrayDivideConquer(nums, mid + 1, right); int crossMax = maxCrossingSum(nums, left, mid, right); return Math.max(Math.max(leftMax, rightMax), crossMax); } private static int maxCrossingSum(int[] nums, int left, int mid, int right) { int sum = 0, leftSum = Integer.MIN_VALUE; for (int i = mid; i >= left; i--) { sum += nums[i]; if (sum > leftSum) leftSum = sum; } sum = 0; int rightSum = Integer.MIN_VALUE; for (int i = mid + 1; i <= right; i++) { sum += nums[i]; if (sum > rightSum) rightSum = sum; } return leftSum + rightSum; }
Complexity: O(n) time, O(1) space
Time Complexity
Kadane's algorithm scans the array once, updating sums in constant time per element, resulting in O(n) time.
Space Complexity
It uses only a few variables for sums, so space complexity is O(1), no extra arrays needed.
Which Approach is Fastest?
Kadane's algorithm is fastest and simplest for this problem compared to brute force O(n^2) or divide and conquer O(n log n).
| Approach | Time | Space | Best For |
|---|---|---|---|
| Kadane's Algorithm | O(n) | O(1) | Large arrays, efficient solution |
| Brute Force | O(n^2) | O(1) | Small arrays, simple implementation |
| Divide and Conquer | O(n log n) | O(log n) | When recursion or parallelism is preferred |