Where the Seesaw Balances
Easy · Prefix Sums · about 10 min
Boxes sit in a row on a long plank; weights[i] is the weight of box i. If you put the pivot under box i, that box counts for neither side.
Return the leftmost position i where the total weight strictly left of i equals the total weight strictly right of i. If there is no such position, return -1.
- A side with no boxes weighs 0.
- Weights are whole numbers and can be zero or negative (think of balloons pulling up).
weightshas 0 to 100,000 numbers.
Examples:
seesaw_balance_index([3, 1, 5, 2, 2]) → 2 (3 + 1 on the left, 2 + 2 on the right)
seesaw_balance_index([1, 2, 3]) → -1
Hints
- If you know the total and the weight on the left, you can work out the weight on the right.
- Right side = total - left - weights[i].
- Add weights[i] to the left side only after you have checked position i.
Learn the pattern first: the Prefix Sums lesson.
More Prefix Sums problems
- Stretches That Hit the Goal (Medium)
- Batches That Split Evenly (Medium)
- Orchard Plot Totals (Medium)
- Plots With an Exact Harvest (Hard)