The golem clash
- O(n²) · Very hard
- Full plan
- Python
- JavaScript
- lists
- loops
- comparisons
Problem
In the arena of Kharos, stone golems do not fight at random: the two heaviest ones left always clash, and so on until no one has a rival.
You get the list of weights (whole numbers greater than 0; it may be empty). While two or more are left, take the two heaviest and make them clash. If they weigh the same, both turn to dust. If not, the lighter one turns to dust and the heavier one stays in the arena, now weighing the difference. If several are tied as the heaviest, two of them clash. Return the weight of the last golem left, as a whole number, or 0 if none is left. With just one, you return its weight; with no golems, 0. With [3, 5, 9]: 9 and 5 clash, leaving one of 4; [3, 4] remain.
4 and 3 clash, leaving one of 1. You return 1.
Examples
The example
[3, 5, 9] → 1
Many clashes
[2, 7, 4, 1, 8, 1] → 1
Just two
[10, 4] → 6
The survivor fights again
[9, 3, 2, 1] → 3
Two equal ones turn to dust
[6, 6] → 0
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def last_golem(weights):
passJavaScript
function lastGolem(weights) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.