Problem 315650 · easy · Level 03 Linear Management & Searching

Seesaw Balance Point

prefix sums · running total

Weights sit in a row on a plank, weights[i] at position i. A position is a balance point if the total weight strictly to its left equals the total weight strictly to its right (the weight at the position itself is ignored; an empty side weighs 0). Return the leftmost balance point, or -1 if there is none.

Examples

Input:  weights = [2, 5, 1, 3, 4]
Output: 2
Explanation: left of index 2 is 2+5 = 7, right of it is 3+4 = 7.

Input:  weights = [4, 3, -3]
Output: 0
Explanation: nothing lies to the left (0) and 3 + (-3) = 0 lies to the right.

Input:  weights = [2, 4, 1]
Output: -1

Constraints

  • 1 <= len(weights) <= 10**5
  • -1000 <= weights[i] <= 1000
  • Target complexity: O(n) time, O(1) extra space.

Goals

  • Derive the right-hand total from the grand total and a running left total
  • Return the leftmost qualifying index
Starting Python…