Weights are placed along a beam at positions 0 .. n-1, given as weights. Return the leftmost index i such that the total weight strictly to the left of i equals the total weight strictly to the right of i. Return -1 if no such index exists. An empty side has weight 0.
Examples
Input: weights = [5, 4, 8, 6, 3]
Output: 2
Explanation: 5 + 4 = 9 and 6 + 3 = 9.
Input: weights = [4, 1, 2]
Output: -1
Input: weights = [7, 3, -3]
Output: 0
Explanation: Nothing is left of index 0 (sum 0) and 3 + (-3) = 0 on the right.
Constraints
0 <= len(weights) <= 10**5-10**4 <= weights[i] <= 10**4- Target: O(n) time, O(1) extra space.
Goals
- Derive the right-hand sum from the total and the left-hand sum
- Avoid recomputing slices inside the loop