Find Equilibrium Index in Java
Problem
An equilibrium index is a position in the array where the sum of every element before it exactly equals the sum of every element after it.
Given an array of integers, find an index where the sum of the elements to its left equals the sum of the elements to its right.
Java Program
public class EquilibriumIndex {
public static void main(String[] args) {
int[] arr = {1, 3, 5, 2, 2};
int total = 0;
for (int num : arr) total += num; // sum the whole array up front
int leftSum = 0;
int equilibriumIndex = -1;
for (int i = 0; i < arr.length; i++) {
int rightSum = total - leftSum - arr[i]; // derived by subtraction, not recomputed
if (leftSum == rightSum) {
equilibriumIndex = i;
break;
}
leftSum += arr[i]; // grow the running left-side total for the next index
}
System.out.println("Equilibrium index: " + equilibriumIndex);
}
}Output
Core Logic
Knowing the array's total sum up front means the right-side sum at any index can be derived by subtraction, instead of being recomputed from scratch every time.
- 1
totalis computed first, by summing every element in the array. - 2
leftSumstarts at0and tracks the running sum of everything before the current index. - 3At each index
i, the right-side sum istotal - leftSum - arr[i]— the total minus everything to the left minus the current element itself. - 4If
leftSumequals that right-side sum,iis the equilibrium index; otherwise,arr[i]is added intoleftSumbefore moving to the next index.
[1, 3, 5, 2, 2], at index 2, leftSum is 1 + 3 = 4 and the right side is 2 + 2 = 4 — equal, so index 2 is reported.Key Point: Deriving the right-side sum by subtraction avoids an inner loop to recompute it at every index, which is what keeps this a single O(n) pass instead of an O(n²) one.
Why: The total is computed in one pass and the equilibrium check in a second pass, both using only a couple of running totals regardless of array size.
Key Concepts
Approach 2: Brute Force
public class EquilibriumIndexBruteForce {
public static void main(String[] args) {
int[] arr = {1, 3, 5, 2, 2};
int equilibriumIndex = -1;
for (int i = 0; i < arr.length; i++) {
int leftSum = 0, rightSum = 0;
for (int j = 0; j < i; j++) leftSum += arr[j]; // recompute from scratch every candidate
for (int j = i + 1; j < arr.length; j++) rightSum += arr[j];
if (leftSum == rightSum) {
equilibriumIndex = i;
break;
}
}
System.out.println("Equilibrium index: " + equilibriumIndex);
}
}
Output
Core Logic
Recomputing the left and right sums from scratch at every candidate index checks the same condition, just without the running-total shortcut.
- 1The outer loop tries every index
ias a candidate equilibrium point. - 2For each candidate, an inner loop sums every element before
iintoleftSum, and another sums every element afteriintorightSum. - 3If
leftSumequalsrightSumfor that index, it's reported as the answer. - 4Every candidate index triggers its own full re-scan of the rest of the array, unlike the running-total version.
[1, 3, 5, 2, 2], checking index 2 sums 1 + 3 = 4 on the left and 2 + 2 = 4 on the right from scratch, confirming the same equilibrium index.Key Point: This checks exactly the same condition as the optimized version, but recomputing both sums at every index is what pushes the cost from O(n) up to O(n²).
Why: Each of the n candidate indices triggers its own linear scan to recompute both the left and right sums from scratch.