Problem 315650 · easy · Phase 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 = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation: left of index 3 is 1+7+3 = 11, right of it is 5+6 = 11.

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

Input:  weights = [1, 2, 3]
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…