labels[i] is the label of box i. The operator wants to reorder the boxes so that no two neighbouring boxes carry the same label. This is guaranteed to be possible.
There are usually many valid orders, so build this particular one:
1. Line the boxes up in a queue: first every box with the most frequent label, then the next most frequent, and so on. Labels that appear equally often go in increasing order.
2. Hand the boxes out from the queue to positions 0, 2, 4, …, and once the even positions run out, to positions 1, 3, 5, ….
Return the resulting row of labels. Think about why it never puts two equal labels side by side.labels=[7, 3, 7, 3, 7] → [7, 3, 7, 3, 7]
Label 7 appears three times and 3 twice, so the queue is 7, 7, 7, 3, 3. Slots 0, 2 and 4 get the 7s, slots 1 and 3 get the 3s.
labels=[5, 2, 9, 2, 5, 9] → [2, 5, 2, 9, 5, 9]
Every label appears twice, so ties are broken by value: 2, 2, 5, 5, 9, 9. The even slots get 2, 2, 5 and the odd slots get 5, 9, 9.
labels=[4] → [4]
labels=[7, 3, 7, 3, 7]
[7, 3, 7, 3, 7]
Run checks the first 3 tests in your browser. Submit checks all 96 tests on the server, hidden ones included.