Java ProgramsArraysFind Equilibrium Index

Find Equilibrium Index in Java

intermediate·  Arrays  ·  Array

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.

Input
[1, 3, 5, 2, 2]
Output
Equilibrium index: 2

Java Program

Java
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

Equilibrium index: 2

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.

How It Works
  1. 1total is computed first, by summing every element in the array.
  2. 2leftSum starts at 0 and tracks the running sum of everything before the current index.
  3. 3At each index i, the right-side sum is total - leftSum - arr[i] — the total minus everything to the left minus the current element itself.
  4. 4If leftSum equals that right-side sum, i is the equilibrium index; otherwise, arr[i] is added into leftSum before moving to the next index.
For [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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

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

running totaltotal array sumfor loop

Approach 2: Brute Force

Java
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

Equilibrium index: 2

Core Logic

Recomputing the left and right sums from scratch at every candidate index checks the same condition, just without the running-total shortcut.

How It Works
  1. 1The outer loop tries every index i as a candidate equilibrium point.
  2. 2For each candidate, an inner loop sums every element before i into leftSum, and another sums every element after i into rightSum.
  3. 3If leftSum equals rightSum for that index, it's reported as the answer.
  4. 4Every candidate index triggers its own full re-scan of the rest of the array, unlike the running-total version.
For [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²).

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: Each of the n candidate indices triggers its own linear scan to recompute both the left and right sums from scratch.

Key Concepts

nested looprecomputed sums

Related Programs