A fruit-packing line receives n crates in a queue, each stamped with an integer weight code. Exactly n/2 of the codes are even and n/2 are odd (n is guaranteed to be even and positive).
The line has n conveyor slots numbered 0 to n-1. Because of how the downstream labeling machine is wired, every even-numbered slot must receive a crate with an even weight code, and every odd-numbered slot must receive a crate with an odd weight code.
Among all rearrangements satisfying that rule, the supervisor wants the one requiring the least disruption to arrival order: the relative order in which the even-coded crates originally arrived must be preserved among themselves, and independently, the relative order in which the odd-coded crates originally arrived must be preserved among themselves. Determine the resulting sequence of weight codes across the conveyor slots.
Line 1: an integer n.
Line 2: n space-separated integers, the weight codes in arrival order.
Print n space-separated integers: the weight codes in slot order 0 to n-1, satisfying the parity and order-preservation rules above.
2 <= n <= 200000, and n is even.0 <= code <= 1000000000n/2 of the input codes are even and exactly n/2 are odd.Example 1
Input
4 4 2 7 1
Expected
4 7 2 1
Explanation
Codes arrive as [4,2,7,1]. Even codes in arrival order are 4,2; odd codes in arrival order are 7,1. Slot 0 (even) gets the first even, 4; slot 1 (odd) gets the first odd, 7; slot 2 (even) gets the second even, 2; slot 3 (odd) gets the second odd, 1. Result: 4 7 2 1.
Example 2
Input
6 5 3 2 8 7 4
Expected
2 5 8 3 4 7
Explanation
Codes arrive as [5,3,2,8,7,4]. Even codes in arrival order are 2,8,4; odd codes in arrival order are 5,3,7. Filling slots 0,2,4 with the evens in that order and slots 1,3,5 with the odds in that order gives: slot0=2, slot1=5, slot2=8, slot3=3, slot4=4, slot5=7. Result: 2 5 8 3 4 7.
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 →