AlgoMaster Logo

Find Pivot Index

Last Updated: March 29, 2026

easy

Understanding the Problem

We need to find an index in the array where everything to its left adds up to the same value as everything to its right. The element at the pivot index itself is not included in either sum.

A couple of things to notice. First, the left sum for index 0 is 0 (nothing to the left), and the right sum for the last index is 0 (nothing to the right). So edge indices are valid candidates. Second, negative numbers are allowed, which means the running sum can decrease as we move right. This rules out any approach that assumes sums are monotonically increasing.

The key observation is this: if we know the total sum of the array, then for any index i, the right sum is just totalSum - leftSum - nums[i]. So the pivot condition becomes leftSum == totalSum - leftSum - nums[i], which simplifies to 2 * leftSum + nums[i] == totalSum. We can check this in a single pass.

Key Constraints:

  • 1 <= nums.length <= 10^4 → With n up to 10,000, even O(n^2) would pass (100 million operations is borderline). But O(n) is clearly preferred and achievable here.
  • -1000 <= nums[i] <= 1000 → Values can be negative. This means sums can decrease, and multiple pivot indices could theoretically exist (we return the leftmost). The bounded range also means the total sum fits comfortably in a 32-bit integer (max ~10^7).

Approach 1: Brute Force

Intuition

The most natural starting point: for each index, compute the left sum and the right sum separately, then check if they're equal. This means two inner loops for every candidate index, which is simple but repetitive.

Algorithm

  1. Iterate through each index i from 0 to n-1.
  2. For each i, compute the left sum by adding all elements from index 0 to i-1.
  3. Compute the right sum by adding all elements from index i+1 to n-1.
  4. If the left sum equals the right sum, return i.
  5. If no pivot is found after checking all indices, return -1.

Example Walkthrough

1i=0: Compute leftSum=0, rightSum=7+3+6+5+6=27. Not equal.
0
1
i
1
7
rightSum=27
2
3
rightSum=27
3
6
rightSum=27
4
5
rightSum=27
5
6
rightSum=27
1/4

Code

This works, but we're recomputing the left sum from scratch at every index. When we move from index i to i+1, the left sum only changes by one element, yet we throw away all that work.

What if we maintained a running left sum and derived the right sum from the total?

Approach 2: Prefix Sum (Optimal)

Intuition

Here's the insight that makes this problem simple. If you know the total sum of the entire array, you can derive the right sum at any index without a separate loop. At index i, the right sum is just totalSum - leftSum - nums[i]. So the pivot condition leftSum == rightSum becomes leftSum == totalSum - leftSum - nums[i].

This means we only need two things: the total sum (computed once upfront) and a running left sum that we build up as we scan from left to right. That's two passes total, or really one pass after the initial sum.

Algorithm

  1. Compute the total sum of the entire array.
  2. Initialize leftSum = 0.
  3. Iterate through each index i:
    • Compute rightSum as totalSum - leftSum - nums[i].
    • If leftSum == rightSum, return i.
    • Add nums[i] to leftSum (preparing for the next index).
  4. If no pivot index was found, return -1.

Example Walkthrough

1Compute totalSum = 1+7+3+6+5+6 = 28, leftSum = 0
0
1
totalSum=28
1
7
totalSum=28
2
3
totalSum=28
3
6
totalSum=28
4
5
totalSum=28
5
6
totalSum=28
1/5

Code