Balanced Stone Removal
Given an array of integers representing the weight of stones in a row, determine how many stones you can remove while keeping the row balanced. A row is considered balanced if, after removing a stone at position i, the sum of the weights of all stones from the start up to but not including i is equal to the sum of the weights of all stones from position i+1 to the end.
[ [ 1, 2, 1, 2, 1 ] ]
Explanation. Removing stones at positions 1, 2, and 4 (zero-based index) keeps the row balanced. For example, after removing the stone at position 2, the sums of the segments [1,2] and [2,1] are both equal to 3.
[ [ 3, 3, 3, 3 ] ]
Explanation. Removing stones at position 1 and 2 keeps the row balanced, splitting the stones into two groups of weight 3 each.
[ [ 10, 20, 10, 3, 2, 1, 3 ] ]
Explanation. Removing the stone at position 2 keeps the row balanced, with sums of 30 on each side.
[ [ 5, 5, 5, 5 ] ]
Explanation. It's not possible to remove any stones while keeping the row balanced because removing any stone will disrupt the balanced state.
Follow-up: Can this problem be solved in linear time complexity?
1. Each integer in the array will be between `1` and `100`. 2. The array will contain between `1` and `1000` elements.
- Views
- 3