Find Pivot Index solution explained
To find the pivot index, compute the total of the list once, then walk left to right keeping a running sum of everything before the current position. The sum to the right is the total minus the left sum minus the current value, so the first index where the two sides match is the answer, or -1 if none does. That is O(n) time and O(1) extra space, against O(n²) for recomputing both sides at every index.
By Evin JohnPublished 6 min read
The task, restated
You get a list of integers, which may include negatives and zeros. Find a position where the sum of all values strictly to its left equals the sum of all values strictly to its right. The value at the position itself belongs to neither side. If several positions qualify, return the leftmost. If none does, return -1. At the first index the left side is empty and counts as 0, and the same holds for the right side at the last index.
| Values | Answer | Why |
|---|---|---|
[2, 7, 1, 5, 4] | 2 | Left of index 2 is 2 + 7 = 9; right is 5 + 4 = 9 |
[5, -5, 3] | 2 | Left of index 2 sums to 0, and the right side is empty |
[4, 1, 6, 2] | -1 | No position balances |
[0, 0, 0] | 0 | Every index balances; the leftmost wins |
[8] | 0 | Both sides are empty, so both are 0 |
The same problem shows up under other names, such as the equilibrium index or the middle index of a list. Confirm two details: whether the pivot value is excluded from both sides (it usually is), and what to return when nothing qualifies.
What the interviewer is looking for
This is an easy problem often used early in a screen. The signal is whether you notice that the right sum can be derived from the total, so you never need to recompute it. Interviewers also listen for the edge cases: empty sides, negative values that make sums go down, and the leftmost rule. A candidate who mentions that a prefix-sum array would also work, and then saves the memory, shows good judgment.
Brute force: sum both sides at every index
def pivot_index_brute(values):
for i in range(len(values)):
if sum(values[:i]) == sum(values[i + 1:]):
return i
return -1Each check costs O(n), and there are n positions, so this is quadratic. The slices also copy data, which adds O(n) memory per check. It is a perfectly good opening line, as long as you move on quickly.
Optimal: one total, one running left sum
Every value is either left of the index, at the index, or right of it. So the right sum is the total minus the left sum minus the current value. Compute the total once. Then walk the list with left starting at 0. At each index compare left with total - left - value. If they match, return the index. Otherwise add the value to left and move on.
You can simplify the comparison to 2 * left + value == total, which some people find tidier. Both forms are fine. Say which you use and why it is the same test.
def pivot_index(values):
total = sum(values)
left = 0
for i, value in enumerate(values):
if left == total - left - value:
return i
left += value
return -1#include <vector>
int pivotIndex(const std::vector<int>& values) {
long long total = 0;
for (int v : values) total += v;
long long left = 0;
for (int i = 0; i < static_cast<int>(values.size()); ++i) {
if (left == total - left - values[i]) return i;
left += values[i];
}
return -1;
}Trace [2, 7, 1, 5, 4]: the total is 19. At index 0, left is 0 and right is 17. At index 1, left is 2 and right is 10. At index 2, left is 9 and right is 19 - 9 - 1 = 9, so the answer is 2.
Cost of each approach
| Approach | Time | Extra space |
|---|---|---|
| Sum both sides at every index | O(n²) | O(n) for the slices |
| Prefix-sum array, then compare | O(n) | O(n) |
| Total plus running left sum | O(n) | O(1) |
Edge cases to test
- An empty list: -1.
- A single value: 0, since both sides are empty.
- A pivot at the first index, where the rest sums to 0, such as
[3, 4, -4]. - A pivot at the last index, as in
[5, -5, 3]. - All zeros: index 0, not some later index.
- Negative values throughout, which make running sums go down.
Common mistakes
- Adding the current value to
leftbefore the comparison, which counts it on the left side. - Assuming values are positive and stopping early when the left sum passes half the total.
- Returning the last matching index instead of the first.
- Using a 32-bit sum in C++ or Java-like languages when values and lengths are large.
- Building two prefix arrays when one total is enough. It works, but it shows you missed the simpler idea.
How to explain it out loud
Fictional example
- Candidate
- I could sum both sides at every index, but that is quadratic. Instead I note that the left sum, the current value and the right sum always add up to the total.
- Candidate
- So I compute the total once and keep a running left sum. At each index the right sum is total minus left minus the value. If the two match, I return the index, and because I scan from the left, it is the leftmost.
- Candidate
- That is one pass after the total, so linear time and constant space. It also works with negative numbers, since I never assume the sums only grow.
Related problems
Minimum Size Subarray Sum also tracks a running sum but needs positive values to move a window. Majority Element II is another single-pass problem where a small amount of state replaces a full table. Trapping Rain Water is a harder cousin that also compares what lies on each side of a position. All walkthroughs are on the coding problems hub.
Where Natively fits in your practice
Easy problems are where you can practice talking while coding. During a mock round on your Mac or Windows computer, Natively transcribes what you say, so you can check afterward whether you named the edge cases or only coded them. If you get stuck, it can read the prompt from a screenshot and suggest an approach, which you can compare with your own once you have finished. The coding interview assistant page lists those features.
Questions people ask
How do you find the pivot index in O(n)?
Compute the total once. Walk the list with a running left sum; at each index the right sum is total minus left minus the current value. Return the first index where they match.
Is the pivot value included in either side?
No. In the usual version, the value at the pivot belongs to neither side. Confirm this with the interviewer.
What if the list has negative numbers?
The total-minus-left method still works, because it never assumes the sums only grow. Just do not stop early when the left sum passes half the total.
What is the pivot index of a single-element list?
0. Both sides are empty, so both sums are 0 and they match.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
