Java Array Program · A27
Find the Equilibrium Index of an Array in Java
Compute the total once. Then, walking left to right, the right-hand sum at any index is the total minus the left sum minus the current element.
Problem Statement
Return the first index i where the sum of the elements before i equals the sum of the elements after i. The element at i belongs to neither side.
An empty side has sum 0. If no such index exists, return -1.
Input/Output Examples
[3, 1, 5, 2, 2]Output2Explanation: 3 + 1 = 4 on the left and 2 + 2 = 4 on the right.[1, 2, 3]Output-1Explanation: no index balances the two sides.[0]Output0Explanation: both sides are empty, so both sums are 0.Visual Explanation
One pass computes the total; a second pass keeps a running left sum. Every right sum is then derived in constant time instead of being added up again.
Approaches
The running-sum approach is linear. The brute force recomputes both sides for every index.
Approach 1: Total and Running Left Sum (Recommended)
Idea: compute the total, then for each index check whether left == total - left - arr[i].
- Sum the whole array into
total. - For each index, compute the right sum as
total - left - arr[i]. - Return the index if both sides match; otherwise add
arr[i]toleft.
Time: O(n) · Space: O(1).
Approach 2: Recompute Both Sides
Idea: for each index, add up the elements on each side from scratch.
- For each index
i, sumarr[0..i-1]andarr[i+1..n-1]. - Return
iif the sums are equal. - Return
-1otherwise.
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: Total and Running Left Sum (Recommended)
Sums are kept in long so large values cannot overflow.
import java.util.Arrays;
public class EquilibriumRunning {
// right sum = total - left sum - current element
static int equilibrium(int[] arr) {
long total = 0;
for (int v : arr) {
total += v;
}
long left = 0;
for (int i = 0; i < arr.length; i++) {
if (left == total - left - arr[i]) {
return i;
}
left += arr[i];
}
return -1;
}
public static void main(String[] args) {
int[][] inputs = {{3, 1, 5, 2, 2}, {1, 2, 3}, {0}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + equilibrium(arr));
}
}
}
[3, 1, 5, 2, 2] -> 2 [1, 2, 3] -> -1 [0] -> 0
Time complexity: O(n) · Space complexity: O(1).
Approach 2: Recompute Both Sides
Easy to verify, but quadratic.
import java.util.Arrays;
public class EquilibriumBrute {
// Recomputes both side sums for every index: O(n^2)
static int equilibrium(int[] arr) {
for (int i = 0; i < arr.length; i++) {
long left = 0;
long right = 0;
for (int j = 0; j < i; j++) {
left += arr[j];
}
for (int j = i + 1; j < arr.length; j++) {
right += arr[j];
}
if (left == right) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[][] inputs = {{3, 1, 5, 2, 2}, {1, 2, 3}, {0}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + equilibrium(arr));
}
}
}
[3, 1, 5, 2, 2] -> 2 [1, 2, 3] -> -1 [0] -> 0
Time complexity: O(n²) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Total and Running Left Sum | O(n) | O(1) |
| Recompute Both Sides | O(n²) | O(1) |
Here n is the number of elements. The running-sum version makes two passes; the brute force adds up to n values for each of n indexes.
Interview Tips and Recommended Approach
- Exclude the current element. It belongs to neither side.
- Treat an empty side as 0. That is why
[0]has index 0. - Use
longsums for large inputs. - Know the prefix-sum idea. It turns many range-sum questions into a subtraction.
Recommended Approach: Total and Running Left Sum
Why: it needs one total and one running sum, giving a linear-time answer with constant memory.