A toll lane's waiting queue is a singly linked list of vehicle IDs. Using 1-based positions along the queue (the first vehicle is at position 1, the second at position 2, and so on), regroup the queue so that all vehicles originally at ODD positions come first, in their original relative order, followed by all vehicles originally at EVEN positions, also in their original relative order. Print the resulting IDs.
Line 1: an integer n — the number of vehicles.
Line 2: n space-separated integers — the vehicle IDs, front to back.
n space-separated integers: the odd-position IDs (in order) followed by the even-position IDs (in order).
Example 1
Input
5 10 20 30 40 50
Expected
10 30 50 20 40
Explanation
Odd positions (1st,3rd,5th) are 10,30,50; even positions (2nd,4th) are 20,40; result: 10 30 50 20 40.
Example 2
Input
2 7 8
Expected
7 8
Explanation
Position 1 (odd) is 7, position 2 (even) is 8, so the result is unchanged: 7 8.
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 →