Loading content...
You are given an array of positive integers boulders, where each element boulders[i] represents the mass of the iᵗʰ boulder.
In this game, on each turn you must select the two heaviest boulders and collide them together. Suppose the two heaviest boulders have masses x and y where x ≤ y. The outcome of the collision follows these rules:
x == y: Both boulders are completely pulverized and removed from the collection.x != y: The lighter boulder (mass x) is completely destroyed, and the heavier boulder's remaining mass becomes y - x.The game continues until at most one boulder remains.
Your task: Determine and return the mass of the final remaining boulder. If all boulders have been pulverized and none remain, return 0.
boulders = [2,7,4,1,8,1]1We select the two heaviest boulders: 8 and 7. After collision: 8 - 7 = 1. Collection becomes [2,4,1,1,1]. Next, select 4 and 2. After collision: 4 - 2 = 2. Collection becomes [2,1,1,1]. Next, select 2 and 1. After collision: 2 - 1 = 1. Collection becomes [1,1,1]. Next, select 1 and 1. They are equal, so both are destroyed. Collection becomes [1]. Only one boulder remains with mass 1.
boulders = [1]1There is only one boulder, so no collisions occur. The remaining mass is 1.
boulders = [5,3]2We collide the only two boulders: 5 and 3. After collision: 5 - 3 = 2. One boulder with mass 2 remains.
Constraints