iq.lab
Python starts when a code cell comes near or you run one
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
Inputnums = [2, 5, 1, 3, 4]Output2

Left of position 2: 2 + 5 = 7. Right of it: 3 + 4 = 7.

Example 2
Inputnums = [1, 2, 3]Output-1

The left and right sums are 0 and 5, then 1 and 3, then 3 and 0. None match.

Example 3
Inputnums = [3, -3, 4]Output2

Left 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.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
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.