Find Pivot Index

Easy

Topics
ArrayPrefix Sum

Given an array nums, return the leftmost pivot index: the index where the sum of all numbers strictly to the left equals the sum strictly to the right. Return -1 if none exists.

Example 1

107132635465
left sumpivotright sum
At index 3 the values to the left sum to the same as the values to the right (the pivot itself counts for neither).
Cells are the array values; the small numbers underneath are indices.
Input:  nums = [1,7,3,6,5,6]
Output: 3

Example 2

102132
No index splits the array into two equal-sum sides, so the answer is -1.
Input:  nums = [1,2,3]
Output: -1

Example 3

2011-12
pivotright sum
Input:  nums = [2,1,-1]
Output: 0

Constraints

  • 1 <= nums.length <= 10^4
  • -1000 <= nums[i] <= 1000
Editorial — intuition, approach, complexity and pitfalls

Intuition

We want an index where the sum strictly left of it equals the sum strictly right of it. The brute force computes both sides for every candidate, which re-adds the same elements repeatedly and costs O(n^2).

The observation that collapses it: the two sides are not independent. If total is the sum of everything and left is the running sum before index i, then the right side is total - left - nums[i] — no second scan needed. One pass carrying left is enough.

This is the prefix-sum idea in its smallest form: maintain one accumulator and derive the complement from the total instead of recomputing it.

Approach

  1. Compute total.
  2. Sweep with left = 0; at each index test left == total - left - nums[i]; return i on the first match.
  3. Add nums[i] to left and continue. Return -1 if none matches.

Dry Run

nums = [1,7,3,6,5,6]   total = 28

i=0  x=1  left=0   right = 28-0-1  = 27   no
i=1  x=7  left=1   right = 28-1-7  = 20   no
i=2  x=3  left=8   right = 28-8-3  = 17   no
i=3  x=6  left=11  right = 28-11-6 = 11   MATCH -> 3

check: 1+7+3 = 11 and 5+6 = 11  ✓

Complexity

  • Time — O(n): one pass for the total, one for the sweep. Against O(n^2) for recomputing both sides per index.
  • Space — O(1): two accumulators. A materialised prefix-sum array would also give O(n) time but at O(n) space for no benefit here, since only the running value is ever read.

Common Pitfalls

  • Including nums[i] in either side. The pivot belongs to neither; that is what the - nums[i] term is for.
  • Adding to left before the comparison, which shifts the test by one index.
  • Returning the first match from the right, or the largest index. The problem asks for the leftmost.
  • Assuming positive values. Negatives are allowed, so you cannot stop early on the grounds that left has grown too large.
  • Treating index 0 or the last index as ineligible. Both are valid, with an empty side summing to 0.

Full Solution — all languages

class Solution:
    def pivotIndex(self, nums):
        total = sum(nums)
        left = 0
        for i, x in enumerate(nums):
            if left == total - left - x:
                return i
            left += x
        return -1
Asked at Expedia IBM Myntra Ola
SensAI
Beta
Tap the mic to stop voice mode