Find Pivot Index
Find Pivot Index is LeetCode 724 (Easy). Given an integer array nums, return its pivot index: the index where the sum of the numbers strictly to its left equals the sum of the numbers strictly to its right.
- The number at the pivot itself counts on neither side.
- If several indices qualify, return the leftmost one. If none does, return
-1. - Values can be negative.
With up to 10,000 numbers, adding up both sides again at every index costs about 10⁸ additions, so the target is one linear pass.
- 1 <= nums.length <= 10⁴
- -1000 <= nums[i] <= 1000
Intuition
Around any index i, the three parts always add up to the same number:
left + nums[i] + right = total
So once the total of the whole array is known, the right sum needs no loop at all: it is total - left - nums[i]. This is a prefix sum trick that keeps only one running sum instead of a whole prefix array.
The search becomes a single sweep. Keep left, the sum of everything before i. At each index, work out the right sum from it and compare the two. The scan runs from index 0 upward, so the first index where they match is the leftmost pivot.
Any question that compares the part of an array before a position with the part after it can use the total: one running sum gives the left part, and subtraction gives the right part.
Approach
Before reading on: if you already know the total of the array and the sum of everything left of index i, how do you get the sum to its right without another loop? Aim for O(n) time and O(1) space.
Add up the whole array once
Compute total = sum(nums) before the loop. Every right sum is worked out from this one number, so the array never has to be added up again.
Work out the right sum by subtraction
At index i, set right = total - left - nums[i]. Subtracting nums[i] takes the pivot out of the right side, because the pivot belongs to neither side.
Compare, then move the number to the left side
If left == right, return i at once; nothing earlier matched, so it is the leftmost pivot. Otherwise add nums[i] to left, because index i sits to the left of every index still to come.
Return -1 when the loop ends
If no index matched, no position splits the array into two equal sides, so the answer is -1.
Find Pivot Index solution in Python | C++ | Java
Common pitfalls
Adding the number to the left sum before the test
left += num
if left == total - left - num:
return iif left == total - left - num:
return i
left += numNow left already holds nums[i], so the pivot is counted on the left side. On [1,7,3,6,5,6] index 3 compares 17 with 5 instead of 11 with 11, and the code returns -1 instead of 3.
Leaving the pivot in the right sum
if left == total - left:
if left == total - left - num:
total - left still contains nums[i]. On [2,1,-1] index 0 compares 0 with 2 instead of 0 with 0, so the real pivot is missed and the code returns -1.
Stopping once the left sum is bigger
if left > right:
breakkeep scanning to the last index
That shortcut only holds when every number is positive. Negatives can shrink the left sum again: on [-1,-1,-1,-1,-1,0] index 0 already has left 0 above right -4, yet index 2 is a pivot (-2 and -2).
Edge cases
[7]Both sides are empty and an empty side sums to 0, so index 0 is the pivot.
[-1,1,5]At index 2 the left sum is -1 + 1 = 0 and the right side is empty, also 0, so the answer is 2. No special case is needed.
Complexity
total, left and right are stored, not a prefix array.Find Pivot Index FAQ
LeetCode 724 and 1991: are they the same problem?
Yes. Find Pivot Index (724) and Find the Middle Index in Array (1991) have exactly the same rules, including returning the leftmost index, so this code passes both.
What is an equilibrium index?
It is another name for the same idea: a position where the sum before it equals the sum after it. Some versions number positions from 1 instead of 0, so check which one the task expects before returning.
Why not build a prefix sum array?
It works: store prefix[i] and read the right sum as prefix[n] - prefix[i + 1]. It needs O(n) extra memory, though, and the total already gives the same right sum with one variable.