Community Problem
Find Pivot Index
Difficulty: Easy
Find the leftmost index where the sum of elements to its left equals the sum to its right.
Find Pivot Index
Find the leftmost index where the sum of elements to its left equals the sum to its right.
By CodeSnatch
March 8, 2026
·
Updated May 20, 2026
1,189 views
33
4.4 (9)
Prefix-sum review with a junior engineer last week, and this is the problem I keep reaching for as the cleanest one-pass intro. The official practice catalog has Range Sum Query and Subarray Sum Equals K, but it skipped the gateway exercise that makes prefix-sum click in under five minutes. So this entry plugs that gap.
Find Pivot Index
Given an integer array nums, return the leftmost pivot index. The pivot index is the index where the sum of all elements strictly to its left equals the sum of all elements strictly to its right. If the index is on the left edge of the array, the left sum is 0 because there are no elements to the left. The same is true for an index on the right edge. If no such index exists, return -1.
Examples
Example 1:
- Input:
nums = [1, 7, 3, 6, 5, 6] - Output:
3 - Explanation: The pivot is index
3. Left sum =1 + 7 + 3 = 11. Right sum =5 + 6 = 11.
Example 2:
- Input:
nums = [1, 2, 3] - Output:
-1 - Explanation: There is no index where the left sum equals the right sum.
Example 3:
- Input:
nums = [2, 1, -1] - Output:
0 - Explanation: The pivot is index
0. Left sum =0(no elements). Right sum =1 + (-1) = 0.
Example 4:
- Input:
nums = [-1, -1, -1, -1, -1, 0] - Output:
2 - Explanation: Left sum =
-1 + -1 = -2. Right sum =-1 + -1 + 0 = -2.
Constraints
1 <= nums.length <= 10^4-1000 <= nums[i] <= 1000
Follow-up
Can you solve this in O(n) time and O(1) extra space, without precomputing a separate prefix-sum array?
Solution
Hints
Solution Walkthrough
Approach: Total Sum, Then Running Left Sum (O(n) time, O(1) space)
The naive idea is to recompute the right sum at every index, which lands you at O(n^2). The trick that turns this into a one-pass O(n) algorithm is the identity:
rightSum(i) = total - leftSum(i) - nums[i]Once you commit to that identity, the algorithm writes itself: compute total once, walk left to right while accumulating leftSum, and at each index check whether leftSum equals total - leftSum - nums[i].
Algorithm
- Compute
total = sum(nums)in one pass. - Initialize
leftSum = 0. - For each index
ifrom0ton - 1:- Compute
rightSum = total - leftSum - nums[i]. - If
leftSum == rightSum, returni. - Otherwise, add
nums[i]toleftSumand continue.
- If the loop ends without a match, return
-1.
Why It Works
The equation leftSum + nums[i] + rightSum = total always holds, by definition of total. Rearranging gives rightSum = total - leftSum - nums[i]. Once leftSum is correct for index i (sum of all elements strictly before i), checking equality against the rearranged right sum is enough. We then update leftSum by adding nums[i] so that on the next iteration it again means the strict-prefix sum.
Complexity
Metric Value
Time O(n), one pass for total, one pass for the scan
Space O(1), only two scalar accumulatorsPitfalls
- Off-by-one on left sum. The check must happen before adding
nums[i]toleftSum, otherwise you are comparing the inclusive prefix to the exclusive right sum and you will miss the answer at the left edge. - Edge indices. When
i = 0the left sum is0because there are no elements to the left. Wheni = n - 1the right sum is0for the same reason. The total/leftSum identity handles both edges automatically, so no special-casing is needed. - Leftmost requirement. The problem asks for the leftmost pivot. Returning on the first match (rather than collecting all matches) is what guarantees that. Iterating right to left would silently fail this requirement.
Solution Code
