A conveyor belt has n slots, numbered 0 through n-1 from left to right, each holding one item of a given weight. The belt has two interleaved lanes: slots at even indices (0, 2, 4, ...) belong to Lane A, and slots at odd indices (1, 3, 5, ...) belong to Lane B. Items never leave their lane, but within a lane they can be rearranged among that lane's slots.
Rearrange the items so that, reading left to right, the weights occupying Lane A's slots are in non-decreasing (ascending) order, and the weights occupying Lane B's slots are in non-increasing (descending) order. Output the resulting full sequence of weights across all n slots.
Line 1: an integer n.
Line 2: n integers w_0 ... w_{n-1}, the weight currently in slot i.
A single line with n integers separated by spaces: the weight in each slot after rearranging, in slot order.
Example 1
Input
4 4 1 2 3
Expected
2 3 4 1
Explanation
Lane A (slots 0,2) holds weights 4 and 2; sorted ascending they become 2 and 4, so slot 0 -> 2 and slot 2 -> 4. Lane B (slots 1,3) holds weights 1 and 3; sorted descending they become 3 and 1, so slot 1 -> 3 and slot 3 -> 1. Result: 2 3 4 1.
Example 2
Input
6 9 3 7 1 5 2
Expected
5 3 7 2 9 1
Explanation
Lane A (slots 0,2,4) holds 9,7,5; sorted ascending: 5,7,9, placed back into slots 0,2,4. Lane B (slots 1,3,5) holds 3,1,2; sorted descending: 3,2,1, placed back into slots 1,3,5. Result: 5 3 7 2 9 1.
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 →