easyPrefix sums target 15 min
Balance point
You get a list of integers nums. Call position i a balance point when the items before i add up to the same total as the items after i. The item at i counts on neither side, and a side with no items adds up to 0.
Return the smallest position that is a balance point. If no position is one, return -1.
Example 1
Input
nums = [2, 5, 1, 3, 4]Output2Left of position 2: 2 + 5 = 7. Right of it: 3 + 4 = 7.
Example 2
Input
nums = [1, 2, 3]Output-1The left and right sums are 0 and 5, then 1 and 3, then 3 and 0. None match.
Example 3
Input
nums = [3, -3, 4]Output2Left of position 2: 3 + (-3) = 0. Nothing is to its right, so that side is 0 too.
Constraints
1 ≤ len(nums) ≤ 105
-1,000 ≤ nums[i] ≤ 1,000
Plan it first
Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.