Find Pivot Index (LeetCode 724): Solution and Explanation
Return the leftmost index where the sum of everything to its left equals the sum of everything to its right, not counting the index itself. Return -1 if no index works.
Intuition
Imagine the array as weights on a seesaw and try putting the fulcrum under each index in turn. The index under the fulcrum does not count for either side. You want the first position where the seesaw balances.
Summing both sides from scratch at every index costs O(n) per index, O(n^2) in total. But the two sides are tied together: everything in the array is either on the left, under the fulcrum, or on the right.
So once you know the total, the right side is total - left - nums[i]. You only need to maintain the left sum, and that grows by one element each time the fulcrum moves right.
Check before you add. At index i the left sum must not include nums[i] yet; add it only after the comparison, when moving on to i + 1.
Approach: Total sum with a running left sum
- Compute
total = sum(nums)and setleft = 0. - For each index
i, computeright = total - left - nums[i]. - If
left == right, returni; this is the leftmost pivot because indices are tried in order. - Otherwise add
nums[i]toleftand continue. - If no index balanced, return -1.
Step-by-step example
nums = [1,7,3,6,5,6]
- The total is 28 and
leftstarts at 0. - i = 0: right = 28 - 0 - 1 = 27. 0 is not 27, so left becomes 1.
- i = 1: right = 28 - 1 - 7 = 20. Not equal, so left becomes 8.
- i = 2: right = 28 - 8 - 3 = 17. Not equal, so left becomes 11.
- i = 3: right = 28 - 11 - 6 = 11. Left is also 11, so the function returns 3.
Code: Python, Java, C++
1class Solution:
2 def pivotIndex(self, nums: List[int]) -> int:
3 total = sum(nums)
4 left = 0
5 for i, x in enumerate(nums):
6 if left == total - left - x:
7 return i
8 left += x
9 return -11class Solution {
2 public int pivotIndex(int[] nums) {
3 int total = 0;
4 for (int x : nums) total += x;
5 int left = 0;
6 for (int i = 0; i < nums.length; i++) {
7 if (left == total - left - nums[i]) {
8 return i;
9 }
10 left += nums[i];
11 }
12 return -1;
13 }
14}1class Solution {
2public:
3 int pivotIndex(vector<int>& nums) {
4 int total = accumulate(nums.begin(), nums.end(), 0);
5 int left = 0;
6 for (int i = 0; i < (int)nums.size(); i++) {
7 if (left == total - left - nums[i]) {
8 return i;
9 }
10 left += nums[i];
11 }
12 return -1;
13 }
14};Time and space complexity
O(n)
One pass computes the total and a second pass moves the fulcrum across the array, each step doing constant work.
O(1)
Only total and left are stored.
Other approaches
Prefix sum array (time O(n), space O(n))
Build prefix[i] = sum of the first i elements. The left sum at i is prefix[i] and the right sum is prefix[n] - prefix[i + 1]. Same idea, but it stores the whole array of prefixes.
Brute force (time O(n^2), space O(1))
For each index, sum the left part and the right part directly. Correct but quadratic, which is slow at 10^4 elements.
Common mistakes
- Adding
nums[i]toleftbefore the comparison, which counts the pivot element on the left side. - Comparing
leftwithtotal - leftinstead oftotal - left - nums[i]. The pivot element belongs to neither side. - Returning the last balanced index instead of the first. Return as soon as a match is found.
- Assuming the values are non-negative. With negative numbers the sums are not monotonic, so you cannot stop early or binary search.
Frequently asked questions
Can the pivot be the first or last index?
Yes. At index 0 the left side is empty and sums to 0, so index 0 is a pivot when the rest of the array sums to 0. The same applies to the last index with an empty right side.
Why can't I use two pointers moving inward?
Two pointers rely on sums growing in a predictable direction. Values here can be negative, so moving a pointer can make a side smaller, and the method can skip the real pivot.
Is this the same as Find the Middle Index in Array?
Yes. LeetCode 1991 asks the exact same question with the same leftmost rule, so the same code solves both.
What is the time complexity of Find Pivot Index?
O(n) time and O(1) extra space: one pass for the total and one pass with a running left sum.