Find Pivot Index: Where Prefix Sum Clicks
Find Pivot Index (#724) is where most people first see prefix sum in action. It is elegantly simple — no hash maps, no nested logic, just one running total and a single insight that makes everything fall into place.
The problem asks you to find the index where the sum of all elements to the left equals the sum of all elements to the right. If you have never worked with prefix sums before, this is the ideal starting point because the pattern is transparent and the code is short.
In this walkthrough you will understand the problem constraints, learn the prefix sum approach that solves it in O(n) time and O(1) space, trace through a concrete example, and handle every edge case interviewers like to throw at you.
Understanding the Find Pivot Index Problem
You are given an integer array nums. You need to find the pivot index — the index where the sum of all numbers strictly to the left equals the sum of all numbers strictly to the right.
Two important details define the boundary behavior. The left sum of index 0 is 0, because there are no elements to its left. Similarly, the right sum of the last index is 0, because there are no elements to its right. This means the pivot can be at the very first or very last position.
If no such index exists, return -1. If multiple pivot indices exist, return the leftmost one. The array can contain negative numbers, which means you cannot make assumptions about sums always increasing.
For example, given nums = [1, 7, 3, 6, 5, 6], the pivot index is 3. The left sum is 1 + 7 + 3 = 11, and the right sum is 5 + 6 = 11. They match, so index 3 is the answer.
The Prefix Sum Approach for Find Pivot Index
The key insight is that you do not need to recompute left and right sums from scratch at every index. Instead, compute the total sum of the array once. Then iterate through the array while maintaining a running leftSum variable.
At each index i, the right sum is simply total - leftSum - nums[i]. If leftSum equals that right sum, you have found your pivot. After checking, add nums[i] to leftSum and move to the next index.
This works because the total sum is fixed. By subtracting the running leftSum and the current element, you get exactly the sum of everything to the right. No extra array needed — just one variable tracking the cumulative left sum.
The time complexity is O(n) for one pass to compute the total and one pass to find the pivot. The space complexity is O(1) because you only store two numbers: total and leftSum.
- Compute totalSum = sum of all elements in one pass
- Initialize leftSum = 0
- For each index i: rightSum = totalSum - leftSum - nums[i]
- If leftSum equals rightSum, return i
- Otherwise add nums[i] to leftSum and continue
- If no pivot found after the loop, return -1
Pro Tip
You don't need a prefix sum array — just track running leftSum and compute rightSum as total - leftSum - nums[i]. One variable, one pass after computing total.
Implementation — Clean and Minimal
The implementation is refreshingly short. First, compute the total sum of the array. Then iterate with a running leftSum, checking the balance condition at each step.
In Python it looks like this: compute total = sum(nums). Set leftSum = 0. Loop through each index i and value num. If leftSum equals total - leftSum - num, return i. Otherwise, leftSum += num. After the loop, return -1.
In JavaScript or TypeScript the logic is identical. The entire function body is five lines. There are no edge-case branches to add because the left-sum-of-zero and right-sum-of-zero properties are handled naturally by the formula.
This is one of those rare problems where the optimal solution is also the simplest one. No sorting, no hash maps, no two pointers — just arithmetic.
Visual Walkthrough with [1, 7, 3, 6, 5, 6]
Let us trace through nums = [1, 7, 3, 6, 5, 6] step by step. The total sum is 1 + 7 + 3 + 6 + 5 + 6 = 28.
At i = 0: leftSum = 0, rightSum = 28 - 0 - 1 = 27. Not equal. Update leftSum to 1.
At i = 1: leftSum = 1, rightSum = 28 - 1 - 7 = 20. Not equal. Update leftSum to 8.
At i = 2: leftSum = 8, rightSum = 28 - 8 - 3 = 17. Not equal. Update leftSum to 11.
At i = 3: leftSum = 11, rightSum = 28 - 11 - 6 = 11. They are equal. Return 3. The pivot index is 3.
- i=0: leftSum=0, rightSum=27 — no match
- i=1: leftSum=1, rightSum=20 — no match
- i=2: leftSum=8, rightSum=17 — no match
- i=3: leftSum=11, rightSum=11 — pivot found
Common Mistake
Watch for the edge case where pivot is at index 0 — left sum is 0 (nothing to the left) and right sum is total - nums[0]. Many candidates forget this case.
Edge Cases You Need to Handle
Pivot at index 0: If the sum of all elements except the first equals 0, then the pivot is at index 0. For example, nums = [2, 1, -1] has a pivot at index 0 because leftSum = 0 and rightSum = 1 + (-1) = 0.
Pivot at the last index: Similarly, if the sum of all elements except the last equals 0, the pivot is at the end. For nums = [-1, 1, 2], the pivot is at index 2.
No pivot exists: Return -1. For nums = [1, 2, 3], no index satisfies the condition. The formula handles this automatically — the loop finishes without returning.
All zeros: Every index is a valid pivot. The problem asks for the leftmost, so return 0. Since leftSum = 0 and rightSum = 0 at index 0, this works without special handling.
Negative numbers: The formula total - leftSum - nums[i] works correctly with negatives. No extra logic is required. This is one of the strengths of the prefix sum approach — it is arithmetic, not conditional.
- Pivot at index 0: leftSum is 0, check if rightSum = total - nums[0] is also 0
- Pivot at last index: rightSum is 0, check if leftSum = total - nums[n-1] is also 0
- No pivot: return -1 after the loop
- All zeros: every index is valid, return 0 (leftmost)
- Negative numbers: formula handles them naturally
What Find Pivot Index Teaches You
Find Pivot Index is the gateway to the prefix sum pattern. The core idea — that you can express a subarray sum as a difference of cumulative sums — shows up in dozens of harder problems.
Once you internalize the "total minus leftSum minus current" trick, you are ready for Subarray Sum Equals K (#560), which layers a hash map on top of the same prefix sum foundation. You will also see it in Range Sum Query (#303) and Product of Array Except Self (#238).
The pattern generalizes beyond simple sums. Any time you need to compare a left partition against a right partition, or compute running aggregates without restarting from scratch, prefix sum thinking applies.
Review this problem with YeetCode flashcards to lock in the pattern. When you see "balance point," "partition," or "left equals right" in a problem statement, your first instinct should be prefix sum — and Find Pivot Index is where that instinct starts.
Why This Problem Matters
Find Pivot Index (#724) is the gentlest introduction to prefix sum — it uses the simplest form (left vs right partition) before the more complex prefix sum + hash map pattern.