An airfield's runway edge-light controller reports n lamp brightness readings, given in the order the lamps currently sit along the runway centerline. The controller's auto-leveling firmware silently dims or brightens any interior lamp whose brightness exactly equals the average of its two immediate neighbors, treating it as a redundant gradient point — and every time that happens it corrupts the maintenance log. Ground crew always avoids this by physically re-seating the lamps according to one fixed rule: sort all n readings into non-decreasing order, then split that sorted list into a low group holding the first ceil(n/2) values (kept in that sorted order) and a high group holding the remaining values (also kept in that sorted order); finally interleave them back into a single row by placing the low group's values, in order, at positions 0, 2, 4, ... and the high group's values, in order, at positions 1, 3, 5, .... Apply this rule to the given readings and report the resulting row.
Print the n brightness readings in the new row order produced by the rule above, space-separated on one line.
Example 1
Input
5 9 1 4 7 3
Expected
1 7 3 9 4
Explanation
Sorting the readings gives 1,3,4,7,9. With n=5, k = ceil(5/2) = 3, so the low group is [1,3,4] and the high group is [7,9]. Interleaving places the low group at positions 0,2,4 and the high group at positions 1,3: position 0 gets 1, position 1 gets 7, position 2 gets 3, position 3 gets 9, position 4 gets 4, giving 1 7 3 9 4.
Example 2
Input
4 2 2 2 2
Expected
2 2 2 2
Explanation
Sorting gives 2,2,2,2. With n=4, k = ceil(4/2) = 2, so the low group is [2,2] and the high group is [2,2]. Since every reading is identical, interleaving them back still produces 2 2 2 2 — the rule is applied the same way regardless of the values being equal.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →