puzzle contents
Contents
raw puzzle

Magic stones

Robert Eisele

Original Problem

You have a number N of magic stones each of which has a level with a positive integer value.

The stones are very heavy and cumbersome to carry around.

Luckily you can use your magic powers to combine two stones of level i to form a single stone of level i+1.

Use your power to minimise the number of stones that you have.

Input

Line 1: An integer N for the number of magic stones you have.
Line 2: N space separated integer values

Output

Line 1: The minimum number of stones you can have.

Constraints

0 < N < 1000

Solution

By first sorting the list of numbers, we can then check if the current and the next number is equal. If so, we could add 1 to the current element and remove the next. After that we start from anew, sort the list and find two identical elements. By combining the traversal and the restart loop into one for loop the result then looks like this in JavaScript:

const N = parseInt(readline());
let levels = readline().split(' ').map(x => parseInt(x, 10));

for (let i = 0; i < levels.length; i++) {
    levels.sort((a, b) => a - b);
    if (levels[i] === levels[i + 1]) {
        levels.splice(i, 2, levels[i] + 1);
        i = -1;
    }
}
print(levels.length);