Find Pivot Index

LeetCode

LeetCode: https://leetcode.com/problems/find-pivot-index/

Given an array of integers nums, return the pivot index where the sum of the numbers to the left equals the sum of the numbers to the right.

1def pivotIndex(nums: List[int]) -> int:
2 total = sum(nums)
3 left = 0
4
5 for i, x in enumerate(nums):
6 if left == total - left - x:
7 return i
8 left += x
9
10 return -1
i
1
0
7
1
3
2
6
3
5
4
6
5
total=28
left=0
Step 1 / 5
Step 1:
Compute total sum, then scan while maintaining left sum.
Pointers: i=0
Focus: select @ [0]
total=28left=0