Given an array of integers nums, calculate the pivot index of this array.
The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.
If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.
Return the leftmost pivot index. If no such index exists, return -1.
Example 1:
Input: nums = [1,7,3,6,5,6] Output: 3 Explanation: The pivot index is 3. Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 Right sum = nums[4] + nums[5] = 5 + 6 = 11
Example 2:
Input: nums = [1,2,3] Output: -1 Explanation: There is no index that satisfies the conditions in the problem statement.
Example 3:
Input: nums = [2,1,-1] Output: 0 Explanation: The pivot index is 0. Left sum = 0 (no elements to the left of index 0) Right sum = nums[1] + nums[2] = 1 + -1 = 0
Constraints:
1 <= nums.length <= 104-1000 <= nums[i] <= 1000Note: This question is the same as 1991: https://leetcode.com/problems/find-the-middle-index-in-array/
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are given a line of numbered boxes, and you want to find a special spot where the total value of all the boxes to the left equals the total value of all the boxes to the right. The brute force approach simply tests every single box one by one, checking if it is the magic balance point.
Here's how the algorithm would work step-by-step:
def find_pivot_index(numbers): # Test every single box one by one to see if it acts as the balance point for current_index in range(len(numbers)): left_summation = 0 right_summation = 0 # Calculate total value of all boxes sitting to the left of the current box for left_index in range(current_index): left_summation += numbers[left_index] # Calculate total value of all boxes sitting to the right of the current box for right_index in range(current_index + 1, len(numbers)): right_summation += numbers[right_index] # Compare left and right totals to check if we found the magic balance point if left_summation == right_summation: return current_index return -1Instead of recalculating sums from scratch for every single position, the optimal approach looks at the big picture first by finding the total combined weight of everything. Then, it walks across the items one by one, keeping a running tally of what has been passed to instantly figure out what remains on the other side.
Here's how the algorithm would work step-by-step:
def pivotIndex(nums):
# Calculate the grand total weight to determine the total sum of all elements.
total_weight = sum(nums)
running_weight = 0
for index, current_item in enumerate(nums):
# Check if the weight behind matches the remaining weight ahead.
if running_weight == total_weight - running_weight - current_item:
return index
# Add the current item weight to the running tally before moving forward.
running_weight += current_item
return -1| Case | How to Handle |
|---|---|
| Empty or null input array | Return negative one immediately since no valid pivot index can exist in a non-existent or empty sequence. |
| Single-element array | Return index zero because the left sum and right sum are both zero, satisfying the pivot condition. |
| Pivot index located at the extreme left edge | Initialize the left sum to zero to correctly represent the absence of elements strictly to the left. |
| Pivot index located at the extreme right edge | Ensure the right sum naturally evaluates to zero when no elements remain to the right of the current index. |
| Array containing negative numbers and zeros | Maintain accurate running sums by using standard arithmetic additions and subtractions that properly handle negative values. |
| Multiple valid pivot indices exist in the array | Iterate strictly from left to right and return the very first index encountered to satisfy the leftmost requirement. |
| No valid pivot index exists in the array | Complete the full traversal without finding a matching condition and return negative one as specified. |
| Very large arrays with extreme integer values | Use standard linear prefix sum tracking to compute values efficiently in O(N) time without risking integer overflow if within language limits. |