Balance Point

EasyRunning Totals≈ 15 min

You are given a list of integers nums. An index is a balance point when the numbers strictly to its left add up to the same total as the numbers strictly to its right. The number at the index itself belongs to neither side, and an empty side adds up to 0.

Return the first balance point, or -1 if there is none.

Examples

text
nums = [1, 7, 3, 6, 5, 6]   →  3
nums = [1, 2, 3]            →  -1
nums = [2, 1, -1]           →  0
nums = [5]                  →  0

In the first example, 1 + 7 + 3 = 11 and 5 + 6 = 11, with the 6 at index 3 in between. In the third, nothing is left of index 0, and 1 + (−1) = 0.

Constraints

  • Between 1 and 20,000 numbers, each between -1,000 and 1,000.
  • Aim for O(n): adding up both sides again at every index costs n².
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 9 on the judge, 5 of them hidden, and measures how your solution scales.