Skip to content
← All posts

Find Pivot Index: Prefix Sum Balance Point

3 min read
leetcodeproblemeasyarrays

Find Pivot Index is LeetCode problem 724. Given an array of integers, find the pivot index where the sum of elements to the left equals the sum of elements to the right. If no such index exists, return -1.

The problem

Given an integer array nums, return the leftmost pivot index. The pivot index is where the sum of all numbers strictly to the left equals the sum of all numbers strictly to the right. If the index is on the left edge, the left sum is 0. Similarly for the right edge.

Example: nums = [1, 7, 3, 6, 5, 6]. The pivot index is 3 because the left sum (1 + 7 + 3 = 11) equals the right sum (5 + 6 = 11).

107132635465pivotleft sum = 11right sum = 11
Array [1, 7, 3, 6, 5, 6]. At index 3, left sum (1 + 7 + 3 = 11) equals right sum (5 + 6 = 11). Index 3 is the pivot.

The approach: prefix sum

Compute the total sum first. Then scan left to right, tracking left_sum. At each index, right_sum = total - left_sum - nums[i]. If left_sum equals right_sum, you found the pivot.

Visual walkthrough

Step 1: Check index 0. left_sum = 0, right_sum = total - 0 - 1 = 27.

107132635465i = 0left_sum = 0right_sum = 270 != 27

0 does not equal 27. Update left_sum to 0 + 1 = 1.

Step 2: Check index 1. left_sum = 1, right_sum = 28 - 1 - 7 = 20.

107132635465i = 1left_sum = 1right_sum = 201 != 20

1 does not equal 20. Update left_sum to 1 + 7 = 8.

Step 3: Check index 2. left_sum = 8, right_sum = 28 - 8 - 3 = 17.

107132635465i = 2left_sum = 8right_sum = 178 != 17

8 does not equal 17. Update left_sum to 8 + 3 = 11.

Step 4: Check index 3. left_sum = 11, right_sum = 28 - 11 - 6 = 11.

107132635465i = 3left_sum = 11right_sum = 11Match!

11 equals 11. Pivot found! Return index 3.

Step 5: (Not reached) Check index 4. left_sum = 17, right_sum = 28 - 17 - 5 = 6.

107132635465i = 4left_sum = 17right_sum = 617 != 6

We already returned at index 3. This step is shown for completeness.

Step 6: (Not reached) Check index 5. left_sum = 22, right_sum = 28 - 22 - 6 = 0.

107132635465i = 5left_sum = 22right_sum = 022 != 0

We already returned at index 3. This step is shown for completeness.

The code

def pivot_index(nums):
    total = sum(nums)
    left_sum = 0

    for i in range(len(nums)):
        if left_sum == total - left_sum - nums[i]:
            return i
        left_sum += nums[i]

    return -1

Key details to notice:

  • You compute total once, then derive right_sum from total, left_sum, and nums[i]
  • left_sum is updated AFTER the check, so it represents the sum of elements strictly to the left
  • The first index with matching sums is returned (leftmost pivot)

The condition left_sum == total - left_sum - nums[i] can also be written as 2 * left_sum + nums[i] == total. Both are equivalent, but the first version makes the intent clearer.

Complexity analysis

MetricValueReasoning
TimeO(n)One pass to compute total, one pass to find the pivot.
SpaceO(1)Only two variables: total and left_sum.

The building blocks

Prefix sums

The prefix sum pattern converts "sum of a range" queries into O(1) lookups. Here you don't store the full prefix array because you only need the running left sum and can derive the right sum from the total.

This pattern appears in:

Edge cases to watch for

  • Pivot at index 0. Left sum is 0. Right sum is total minus nums[0]. If they match, return 0.
  • Pivot at last index. Right sum is 0. Left sum is total minus nums[-1].
  • Negative numbers. The algorithm handles negatives naturally since it uses sum arithmetic.
  • Single element. [1]. Left sum = 0, right sum = 0. Return 0.
  • No pivot exists. [1, 2, 3]. Return -1.

From understanding to recall

Find Pivot Index is a clean application of the prefix sum pattern. The key insight is that you do not need to store a prefix sum array because you can derive the right sum from the total and the running left sum. This "total minus partial" trick appears frequently in array problems.

Reading the solution once gives you the idea, but writing it from scratch under pressure requires remembering the exact update order: check first, then increment left_sum. Spaced repetition drills lock in these ordering details so you do not second-guess yourself during interviews.

Related posts


Visual Learner? Explore how prefix sum algorithms work with our Array Interactive Visualization.