Find Pivot Index — Solution in Python, Java & C++
Find Pivot Index solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on arrays and prefix sum.
Find Pivot Index is an easy coding problem on arrays and prefix sum, asked in Amazon, Google, Adobe and Meta interviews.
- Difficulty: Easy
- Topics: Arrays, Prefix Sum
- Asked at: Amazon, Google, Adobe, Meta, TCS, Myntra, Ola, Indeed
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an array of integers nums, return the leftmost pivot index: the index where the sum of all numbers strictly to its left equals the sum of all numbers strictly to its right.
At index 0 the left sum is 0, and at the last index the right sum is 0. If no pivot index exists, return -1.
Example 1
Input: nums = [1,7,3,6,5,6]
Output: 3
Explanation: 1+7+3 = 11 on the left and 5+6 = 11 on the right.
Example 2
Input: nums = [1,2,3]
Output: -1
Example 3
Input: nums = [2,1,-1]
Output: 0
Explanation: The left sum is 0 and the right sum is 1 + (-1) = 0.
Constraints
1 <= nums.length <= 40-1000 <= nums[i] <= 1000
Hints for Find Pivot Index
Hint 1
Recomputing both sides at every index is O(n²) — the total never changes, so compute it once.
Hint 2
Keep a running left sum; the right sum is then
total - left - nums[i], available in O(1).Hint 3
Return at the first match, since the problem asks for the leftmost such index.
How to solve Find Pivot Index
The total never changes, so with a running left sum the right sum is total - left - nums[i] and each index is tested in O(1).
Approach
- Compute
total. - Walk
iwithleft = 0; ifleft == total - left - nums[i], returni. - Add
nums[i]toleft; return-1after the loop.
Why it works
At index i the left sum excludes nums[i] and the right sum is everything else, which is exactly the identity used. Returning at the first match gives the leftmost pivot.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Add
nums[i]toleftafter the comparison. - Index 0 with a zero-sum remainder is a valid pivot.
Reference solution
Python
def pivotIndex(nums):
total = sum(nums)
left = 0
for i, x in enumerate(nums):
if left == total - left - x:
return i
left += x
return -1Java
public static 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;
}C++
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;
}JavaScript
var pivotIndex = function(nums) {
let total = 0;
for (let i = 0; i < nums.length; i++) total += nums[i];
let left = 0;
for (let i = 0; i < nums.length; i++) {
if (left === total - left - nums[i]) return i;
left += nums[i];
}
return -1;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Prefix Sum