An index i is a balance point when the sum of the values strictly to its left equals the sum of the values strictly to its right. The value at i itself belongs to neither side.
At index 0 there is nothing to the left, so the left sum is 0; the same applies at the last index for the right sum.
Return the leftmost balance point, or -1 if the array has none.
Examples
Example 1
Input: nums = [1,7,3,6,5,6]
Output: 3
Explanation: At index 3 the left sum is 1 + 7 + 3 = 11 and the right sum is 5 + 6 = 11. No smaller index balances.
Example 2
Input: nums = [1,2,3]
Output: -1
Explanation: No index has equal sums on both sides.
Example 3
Input: nums = [2,1,-1]
Output: 0
Explanation: At index 0 the left sum is 0 (nothing is to the left) and the right sum is 1 + (-1) = 0.
Constraints
1≤nums.length≤104
−1000≤nums[i]≤1000
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up