A regatta crew has a queue of n numbered marker buoys to drop along the course, in a fixed queue order. Race rules require every even-coded buoy to be dropped before any odd-coded buoy. Among buoys sharing the same parity, the crew must drop them in the same relative order they appear in the original queue, since the radio log must stay consistent with the original numbering. Given the queue, output the order in which the buoys should actually be dropped.
The first line contains a single integer n, the number of buoys. The second line contains n space-separated integers a_1 ... a_n, the buoy codes in original queue order.
Print the n buoy codes, space-separated on one line, in the required drop order: every even code first (in original relative order), followed by every odd code (in original relative order).
Example 1
Input
6 3 1 4 1 5 9
Expected
4 3 1 1 5 9
Explanation
The queue is 3, 1, 4, 1, 5, 9. Only 4 (position 3) is even, so it moves to the front unchanged. The remaining buoys 3, 1, 1, 5, 9 are all odd and keep their original relative order, giving 4 3 1 1 5 9.
Example 2
Input
5 2 4 6 8 10
Expected
2 4 6 8 10
Explanation
Every buoy code is already even, so no reordering is needed and the required drop order is identical to the input order: 2 4 6 8 10.
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 →