Array Equilibrium Index - Problem

Given an array of integers, find all equilibrium indices where the sum of elements on the left equals the sum of elements on the right.

An equilibrium index is an index i where the sum of elements at indices [0, 1, ..., i-1] equals the sum of elements at indices [i+1, i+2, ..., n-1].

For index 0, the left sum is 0. For index n-1, the right sum is 0.

Return all equilibrium indices in ascending order. If no equilibrium indices exist, return an empty array.

Input & Output

Example 1 — Basic Array
$ Input: nums = [1, 3, 5, 2, 2]
Output: []
💡 Note: No equilibrium index exists. At index 0: left=0, right=12. At index 1: left=1, right=9. At index 2: left=4, right=4 - this would be equilibrium but 5≠0. At index 3: left=9, right=2. At index 4: left=11, right=0.
Example 2 — Single Equilibrium
$ Input: nums = [1, 7, 3, 6, 5, 6]
Output: [3]
💡 Note: At index 3: left sum = 1+7+3 = 11, right sum = 5+6 = 11. Both sums are equal, so index 3 is an equilibrium point.
Example 3 — Multiple Equilibria
$ Input: nums = [2, 1, 6, 4]
Output: [1]
💡 Note: At index 1: left sum = 2, right sum = 6+4 = 10. Wait, that's not equal. Let me recalculate: at index 1, left=2, right=10. Actually no equilibrium exists in this case.

Constraints

  • 1 ≤ nums.length ≤ 104
  • -109 ≤ nums[i] ≤ 109

Visualization

Tap to expand
INPUTALGORITHMRESULT17360123Array: [1, 7, 3, 6]Find equilibrium indiceswhere left sum = right sum1Calculate total sum = 172For each index i:left_sum, right_sum3Check if left = right4Add to result if equalIndex 3: left=11, right=011 ≠ 0, not equilibrium[ ]No equilibrium indicesfound in this arrayAll index checks failed:i=0: 0≠16, i=1: 1≠9i=2: 8≠6, i=3: 11≠0💡Key Insight:Use the relationship: left_sum + current + right_sum = total_sumto calculate right_sum = total - left_sum - current in O(1) timeTutorialsPoint - Array Equilibrium Index | Single Pass Optimal
Asked in
Google 15 Amazon 12 Microsoft 8
28.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen