Find the Equilibrium Index of an Array
Solve this Problem
Given an array
nums, find the leftmost index where the sum of every element strictly to its left equals the sum of every element strictly to its right — an equilibrium indexEquilibrium IndexAn index that splits the array into two parts (excluding the index itself) whose sums are equal. At index 0 the left part is empty (sum 0); at the last index the right part is empty.. Return -1 if none exists.
Recomputing both sums from scratch for every candidate index wastes the fact that they barely change between consecutive candidates. Compute the array's total sum once, and the right-hand sum at any index can be derived instantly as total - leftSum - nums[i] — turning an O(n²) scan into a single O(n) pass.
Test Case 1:
Input:nums = [1, 7, 3, 6, 5, 6]
Output:3
Explanation:Everything left of index 3 sums to 1+7+3=11, and everything right of it sums to 5+6=11.
Test Case 2:
Input:nums = [1, 2, 3]
Output:-1
Explanation:No index splits the array into two equal-sum halves.
Test Case 3:
Input:nums = [2, 1, -1]
Output:0
Explanation:At index 0 there's nothing to the left (sum 0), and 1 + -1 = 0 to the right — they match.
Constraints
- ◆
1 ≤ nums.length ≤ 10⁴ - ◆
-1000 ≤ nums[i] ≤ 1000
🚀
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
🧪Try your own test case
| 1 | class Solution { |
| 2 | public int findEquilibriumIndex(int[] nums) { |
| 3 | int total = 0; |
| 4 | for (int n : nums) total += n; |
| 5 | int leftSum = 0; |
| 6 | for (int i = 0; i < nums.length; i++) { |
| 7 | int rightSum = total - leftSum - nums[i]; |
| 8 | if (leftSum == rightSum) return i; |
| 9 | leftSum += nums[i]; |
| 10 | } |
| 11 | return -1; |
| 12 | } |
| 13 | } |
| 14 |
1
7
3
6
5
6
0
1
2
3
4
5
Variables
total
28leftSum
0total = nums[0..5]
= 1 + 7 + 3 + 6 + 5 + 6
= 28
INITIALIZE
Compute the total sum once: 28. Start leftSum at 0 and walk the array from index 0.
Step 1 / 12
Approach & Solutions
Brute Force
BruteFor each candidate index, sum everything to its left and everything to its right from scratch and compare the two. Correct, but every candidate re-scans almost the entire array — work a single pass could avoid.
Time
O(n²)Space
O(1)Java
1class Solution {
2 public int findEquilibriumIndex(int[] nums) {
3 for (int i = 0; i < nums.length; i++) {
4 int leftSum = 0, rightSum = 0;
5 for (int j = 0; j < i; j++) leftSum += nums[j];
6 for (int j = i + 1; j < nums.length; j++) rightSum += nums[j];
7 if (leftSum == rightSum) return i;
8 }
9 return -1;
10 }
11}Optimal — Prefix Sum
OptimalCompute the total sum of the array once. Then walk left to right keeping a running leftSum — at each index, rightSum can be derived instantly as total - leftSum - nums[i], with no rescanning needed. Compare the two, and fold nums[i] into leftSum before moving on.
Time
O(n)Space
O(1)Java
1class Solution {
2 public int findEquilibriumIndex(int[] nums) {
3 int total = 0;
4 for (int n : nums) total += n;
5 int leftSum = 0;
6 for (int i = 0; i < nums.length; i++) {
7 int rightSum = total - leftSum - nums[i];
8 if (leftSum == rightSum) return i;
9 leftSum += nums[i];
10 }
11 return -1;
12 }
13}