Java Array Program · A50
Maximum Circular Subarray Sum in Java
A circular subarray either does not wrap, which ordinary Kadane finds, or it wraps, in which case it is everything except a middle part. Make that middle part as small as possible.
Problem Statement
Treat the array as circular: after the last element comes the first. Return the largest sum of a non-empty subarray, which may wrap around, using each element at most once.
If every value is negative, the answer is the largest single value.
Input/Output Examples
[8, -4, 3, -2, 6]Output15Explanation: 3, -2, 6, 8 wraps around and skips -4.[-3, -2, -5]Output-2Explanation: the best non-empty subarray is the single -2.[2, -1, 3]Output5Explanation: 3 followed by 2, skipping -1.Visual Explanation
- Kadane maxno wrap8 -4 3 -2 6the whole array is best 11
- Kadane minmiddle-4smallest subarray -4
- Wraptotal − min11 − (−4)everything except -4 15
- Donemax(11, 15)15
[8, -4, 3, -2, 6]: the best non-wrapping subarray is the whole array (11), but dropping the smallest middle part, -4, gives the wrapping subarray 3, -2, 6, 8 with sum 15.Both Kadane passes run in the same loop. The all-negative case needs a guard: there, total minus minimum would mean taking no elements at all, which is not allowed.
Approaches
The circular Kadane method is linear. The brute force tries every start and length.
Approach 1: Kadane Max and Min (Recommended)
Idea: compute the maximum subarray, the minimum subarray and the total in one pass; the answer is max(maxSub, total - minSub).
- Run Kadane for the maximum and, at the same time, for the minimum.
- Keep the total of all elements.
- If the maximum is negative, return it.
- Otherwise return
max(maxSub, total - minSub).
Time: O(n) · Space: O(1).
Approach 2: Every Start and Length
Idea: for each start, add up to n elements using % n to wrap.
- For each start, reset the sum.
- Add
arr[(start + len - 1) % n]forlenfrom 1 ton. - Keep the best sum.
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 Max and Min (Recommended)
The guard for negative maxima handles the all-negative case.
import java.util.Arrays;
public class CircularKadane {
// A wrapping subarray = total minus a middle part; make that middle part as small as possible
static int maxCircular(int[] arr) {
int total = 0;
int curMax = 0, best = Integer.MIN_VALUE;
int curMin = 0, worst = Integer.MAX_VALUE;
for (int v : arr) {
total += v;
curMax = Math.max(v, curMax + v);
best = Math.max(best, curMax);
curMin = Math.min(v, curMin + v);
worst = Math.min(worst, curMin);
}
if (best < 0) {
return best; // all negative: wrapping would mean an empty subarray
}
return Math.max(best, total - worst);
}
public static void main(String[] args) {
int[][] inputs = {{8, -4, 3, -2, 6}, {-3, -2, -5}, {2, -1, 3}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + maxCircular(arr));
}
}
}
[8, -4, 3, -2, 6] -> 15 [-3, -2, -5] -> -2 [2, -1, 3] -> 5
Time complexity: O(n) · Space complexity: O(1).
Approach 2: Every Start and Length
Directly models the circular definition, so it is a good reference.
import java.util.Arrays;
public class CircularBrute {
// Tries every start and every length up to n, wrapping with % n: O(n^2)
static int maxCircular(int[] arr) {
int n = arr.length;
int best = Integer.MIN_VALUE;
for (int start = 0; start < n; start++) {
int sum = 0;
for (int len = 1; len <= n; len++) {
sum += arr[(start + len - 1) % n];
best = Math.max(best, sum);
}
}
return best;
}
public static void main(String[] args) {
int[][] inputs = {{8, -4, 3, -2, 6}, {-3, -2, -5}, {2, -1, 3}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + maxCircular(arr));
}
}
}
[8, -4, 3, -2, 6] -> 15 [-3, -2, -5] -> -2 [2, -1, 3] -> 5
Time complexity: O(n²) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Kadane Max and Min | O(n) | O(1) |
| Every Start and Length | O(n²) | O(1) |
Here n is the number of elements. The Kadane version reads each value once; the brute force considers n² circular subarrays.
Interview Tips and Recommended Approach
- Explain the two cases. Non-wrapping uses plain Kadane; wrapping is total minus a middle part.
- Guard the all-negative case. Otherwise the answer becomes 0, an empty subarray.
- Do not double the array. Concatenating it and running Kadane can use an element twice.
- Compute everything in one pass to keep the code short.
Recommended Approach: Kadane Max and Min
Why: it reduces the circular problem to two Kadane runs and a subtraction, all in one linear pass with constant memory.