Find Pivot Index
Find Pivot Index is a easy arrays & hashing problem solved with the prefix sum strategy pattern.
The best approach, optimal (total sum + running left sum), runs in O(n) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force (sum both sides for every index) up.
Problem
You are given an integer array nums. A pivot index is an index i where the sum of all elements strictly to the left of i equals the sum of all elements strictly to the right of i. An empty side counts as a sum of 0.
Return the leftmost pivot index, or -1 if no index balances the array.
Examples
Example 1
- Input
nums = [2, 5, 1, 4, 3]- Output
2- Why
- Left of index 2 the sum is 2 + 5 = 7, and right of it the sum is 4 + 3 = 7.
Example 2
- Input
nums = [3, 1, 2]- Output
-1- Why
- Index 0 gives 0 vs 3, index 1 gives 3 vs 2, index 2 gives 4 vs 0. None balance.
Example 3
- Input
nums = [6, -2, 2]- Output
0- Why
- Nothing is to the left of index 0 (sum 0), and -2 + 2 = 0 on the right.
Constraints
1 <= nums.length <= 10^4-1000 <= nums[i] <= 1000
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (3 examples, or your own). Submit also runs 13 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Brute force (sum both sides for every index) | O(n²) | O(1) |
| Optimal (total sum + running left sum) | O(n) | O(1) |
1Brute force (sum both sides for every index)
O(n²)Each of the n indices does a full O(n) pass to build both sums.O(1)Only a few counters.For every index, add up everything to its left and everything to its right and compare.
- For i from 0 to n - 1:
- left = sum of nums[0..i-1], right = sum of nums[i+1..n-1].
- If left == right, return i.
- Return -1 after the loop.
class Solution {
public int pivotIndex(int[] nums) {
int n = nums.length;
for (int i = 0; i < n; i++) {
int left = 0, right = 0;
for (int j = 0; j < i; j++) left += nums[j];
for (int j = i + 1; j < n; j++) right += nums[j];
if (left == right) return i;
}
return -1;
}
}class Solution:
def pivotIndex(self, nums: List[int]) -> int:
n = len(nums)
for i in range(n):
left = 0
right = 0
for j in range(i):
left += nums[j]
for j in range(i + 1, n):
right += nums[j]
if left == right:
return i
return -1class Solution {
public:
int pivotIndex(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n; i++) {
int left = 0, right = 0;
for (int j = 0; j < i; j++) left += nums[j];
for (int j = i + 1; j < n; j++) right += nums[j];
if (left == right) return i;
}
return -1;
}
};function pivotIndex(nums) {
const n = nums.length;
for (let i = 0; i < n; i++) {
let left = 0, right = 0;
for (let j = 0; j < i; j++) left += nums[j];
for (let j = i + 1; j < n; j++) right += nums[j];
if (left === right) return i;
}
return -1;
}2Optimal (total sum + running left sum)
O(n)One pass for the total and one pass for the scan.O(1)Two running sums.Compute the total once. Walk left to right keeping left, the sum of everything before the current index. The right side is then total - left - nums[i], so each index is checked in O(1).
- total = sum of all elements; left = 0.
- For each i: if left == total - left - nums[i], return i.
- Otherwise left += nums[i].
- Return -1.
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;
}
}class Solution:
def pivotIndex(self, nums: List[int]) -> int:
total = sum(nums)
left = 0
for i, x in enumerate(nums):
if left == total - left - x:
return i
left += x
return -1class 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;
}
};function pivotIndex(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;
}Edge cases to test
- A single element: index 0 is always the pivot (both sides are empty)
- The pivot is the first or the last index
- Several indices balance: return the leftmost one
- Negative values, so the running sum is not monotonic
Hints
Hint 1
For a fixed index, the left sum plus the right sum plus nums[i] equals the total.
Hint 2
Keep a running left sum while you scan; the right sum is then total - left - nums[i].
FAQ
What is the best time complexity for Find Pivot Index?
Optimal (total sum + running left sum) runs in O(n) time and O(1) extra space. One pass for the total and one pass for the scan.
Which pattern does Find Pivot Index use?
It is a arrays & hashing problem that uses the prefix sum strategy pattern. Other problems with the same pattern: Subarray Sum Equals K, Subarray with given XOR, Range Sum Query - Immutable.
Is there a brute force solution for Find Pivot Index?
Yes. Brute force (sum both sides for every index) takes O(n²) time and O(1) space. For every index, add up everything to its left and everything to its right and compare.
Which edge cases should I test for Find Pivot Index?
A single element: index 0 is always the pivot (both sides are empty); The pivot is the first or the last index; Several indices balance: return the leftmost one; Negative values, so the running sum is not monotonic.