Ctrl K

Smash to the Smallest Stone

Medium
You have stones with weights weights. In one move you pick any two stones with weights x ≤ y and smash them together: - if x == y, both disappear; - otherwise stone x disappears and stone y now weighs y - x. Moves continue while more than one stone is left. Return the smallest possible weight of the stone that remains, or 0 if none is left.
Example 1
weights=[6, 4, 3, 2, 8] → 1

The total 23 is odd, so zero is impossible. Groups {6, 4, 2} and {3, 8} weigh 12 and 11, so a remainder of 1 is reachable, e.g. 8 - 6 = 2, 4 - 3 = 1, 2 - 2 = 0, and a stone of 1 is left.

Example 2
weights=[5, 5] → 0

Two equal stones destroy each other, leaving 0.

Example 3
weights=[40, 9, 7] → 24
Constraints
  • 1 ≤ len(weights) ≤ 30
  • 1 ≤ weights[i] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
weights=[6, 4, 3, 2, 8]
Expected
1

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