Find Pivot Index
Easy
Topics
Given an array nums, return the leftmost pivot index: the index where the sum of all numbers strictly to the left equals the sum strictly to the right. Return -1 if none exists.
Example 1
Input: nums = [1,7,3,6,5,6] Output: 3
Example 2
-1.Input: nums = [1,2,3] Output: -1
Example 3
Input: nums = [2,1,-1] Output: 0
Constraints
- 1 <= nums.length <= 10^4
- -1000 <= nums[i] <= 1000
Editorial — intuition, approach, complexity and pitfalls
Intuition
We want an index where the sum strictly left of it equals the sum strictly right of it. The brute force computes both sides for every candidate, which re-adds the same elements repeatedly and costs O(n^2).
The observation that collapses it: the two sides are not independent. If total is the sum of everything and left is the running sum before index i, then the right side is total - left - nums[i] — no second scan needed. One pass carrying left is enough.
This is the prefix-sum idea in its smallest form: maintain one accumulator and derive the complement from the total instead of recomputing it.
Approach
- Compute
total. - Sweep with
left = 0; at each index testleft == total - left - nums[i]; returnion the first match. - Add
nums[i]toleftand continue. Return -1 if none matches.
Dry Run
nums = [1,7,3,6,5,6] total = 28
i=0 x=1 left=0 right = 28-0-1 = 27 no
i=1 x=7 left=1 right = 28-1-7 = 20 no
i=2 x=3 left=8 right = 28-8-3 = 17 no
i=3 x=6 left=11 right = 28-11-6 = 11 MATCH -> 3
check: 1+7+3 = 11 and 5+6 = 11 ✓Complexity
- Time — O(n): one pass for the total, one for the sweep. Against O(n^2) for recomputing both sides per index.
- Space — O(1): two accumulators. A materialised prefix-sum array would also give O(n) time but at O(n) space for no benefit here, since only the running value is ever read.
Common Pitfalls
- Including
nums[i]in either side. The pivot belongs to neither; that is what the- nums[i]term is for. - Adding to
leftbefore the comparison, which shifts the test by one index. - Returning the first match from the right, or the largest index. The problem asks for the leftmost.
- Assuming positive values. Negatives are allowed, so you cannot stop early on the grounds that
lefthas grown too large. - Treating index 0 or the last index as ineligible. Both are valid, with an empty side summing to 0.
Full Solution — all languages
class Solution:
def pivotIndex(self, nums):
total = sum(nums)
left = 0
for i, x in enumerate(nums):
if left == total - left - x:
return i
left += x
return -1import java.util.*;
class Solution {
public int pivotIndex(int[] nums) {
int total = 0;
for (int x : nums) total += x;
int left = 0;
for (int i = 0; i < nums.length; i++) {
if (left == total - left - nums[i]) return i;
left += nums[i];
}
return -1;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int pivotIndex(vector<int>& nums) {
int total = 0;
for (int x : nums) total += x;
int left = 0;
for (int i = 0; i < (int)nums.size(); i++) {
if (left == total - left - nums[i]) return i;
left += nums[i];
}
return -1;
}
};/**
* @param {number[]} nums
* @return {number}
*/ var pivotIndex = function(nums) {
let total = 0;
for (const x of nums) total += x;
let left = 0;
for (let i = 0; i < nums.length; i++) {
if (left === total - left - nums[i]) return i;
left += nums[i];
}
return -1;
};