Before every launch, a coaster operator rebalances the single-file row of cars numbered 1 through n by swapping the riders in each adjacent pair of cars: the rider in car 1 swaps with the rider in car 2, the rider in car 3 swaps with the rider in car 4, and so on. If n is odd, the rider in the very last car has no partner and keeps their original seat. Given the rider ID currently sitting in each car, in car order, report the rider ID sitting in each car after this one round of pairwise swaps.
Line 1: an integer n, the number of cars.
Line 2: n space-separated integers, the rider ID in car 1, 2, ..., n respectively.
A single line with n space-separated integers: the rider ID in each car, in car order, after the swap.
1 <= n <= 1000001 and 1000000000, inclusive. IDs are not guaranteed distinct.Example 1
Input
5 10 20 30 40 50
Expected
20 10 40 30 50
Explanation
Cars 1&2 swap (10,20 -> 20,10), cars 3&4 swap (30,40 -> 40,30), and car 5 has no partner so rider 50 stays put. Result: 20 10 40 30 50.
Example 2
Input
4 1 2 3 4
Expected
2 1 4 3
Explanation
Cars 1&2 swap (1,2 -> 2,1) and cars 3&4 swap (3,4 -> 4,3), giving every car a partner since n is even. Result: 2 1 4 3.
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 →