Find Pivot Index
Easy · prefix sum
You are given a list of integers nums, which may contain negative numbers and zeros.
A balance point is a position in the list where the numbers strictly to its left add up to the same total as the numbers strictly to its right. The number at the position itself is not part of either side. If there is nothing on one side, that side adds up to 0.
Return the leftmost balance point, as a zero-based index. If the list has no balance point, return -1.
Examples
Example 1
Input: nums = [1, 7, 3, 6, 5, 6]
Output: 3
Left of index 3 is 1 + 7 + 3 = 11, and right of it is 5 + 6 = 11.
Example 2
Input: nums = [1, 2, 3]
Output: -1
No position has equal totals on both sides.
Example 3
Input: nums = [2, 1, -1]
Output: 0
Nothing is left of index 0, so that side is 0, and the right side is 1 + (-1) = 0.
Constraints
- 1 <= len(nums) <= 10^4
- -1000 <= nums[i] <= 1000