Ctrl K

Balance Point

Easy
The integer array weights lists loads laid out in a row at positions 0, 1, 2, ... If you put a support under position i, load i itself goes to neither side, and the sum on the left must equal the sum on the right. An empty side weighs 0. Loads may be negative. Return the leftmost position where the row balances, or -1 if there is none.
Example 1
weights=[3, 4, 9, 1, 6] → 2

Left of index 2 sits 3 + 4 = 7, right of it 1 + 6 = 7. No earlier index works: at index 0 the left side is 0 and the right side is 20.

Example 2
weights=[-2, 5, -5] → 0

At index 0 the left side is empty (sum 0) and the right side is 5 + (-5) = 0, so it already works.

Example 3
weights=[1, 2, 3] → -1
Constraints
  • 1 ≤ len(weights) ≤ 10⁵
  • -1000 ≤ weights[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBalance Point Index EasyThe Battery Must Not Run Dry EasyBalance After Each Day EasyTotals of Odd-Length Slices EasyThe Hiker's Highest Point Easy
loading Python…
Input
weights=[3, 4, 9, 1, 6]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 96 tests on the server, hidden ones included.