A parts rack holds n widgets (n is always even), each with a known weight. Two robotic arms, A and B, empty the rack in rounds until it is bare. In each round: arm A first pulls the single lightest widget remaining on the rack and sets it aside; then arm B pulls the new lightest widget remaining on the rack (after A's widget is gone) and sets it aside too. The round then places B's widget onto the output belt, immediately followed by A's widget. This repeats, round after round, until the rack is empty.
Given the initial weights of the n widgets, report the sequence of weights as they land on the belt.
Line 1: an integer n. Line 2: n integers, the weight of each widget.
A single line with n integers, separated by spaces: the weights in the order they land on the belt.
Example 1
Input
4 5 4 2 3
Expected
3 2 5 4
Explanation
Sorted ascending the widgets are [2,3,4,5]. Round 1: arm A pulls 2, arm B pulls the next smallest, 3; the belt receives B's 3 then A's 2, giving [3,2]. Round 2: the rack now holds [4,5]; arm A pulls 4, arm B pulls 5; the belt receives 5 then 4. Final belt order: 3 2 5 4.
Example 2
Input
2 2 5
Expected
5 2
Explanation
Only one round: arm A pulls the smaller weight 2, arm B pulls the remaining weight 5. The belt receives B's widget first, then A's: 5 2.
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 →