#C14514. Rearrange Even and Odd

    ID: 44172 Type: Default 1000ms 256MiB

Rearrange Even and Odd

Rearrange Even and Odd

Given a list of integers, your task is to rearrange the list so that all even numbers appear before all odd numbers, while preserving the relative order of the numbers within each group. The input is provided in a single line where the first integer indicates n, the number of elements, followed by n integers.

The output should be a single line containing the rearranged list of integers, separated by spaces.

For example, given the input: 4 3 1 2 4, the output should be: 2 4 3 1.

Mathematically, if \(a_1, a_2, \ldots, a_n\) represents the sequence, let \(E = \{a_i \mid a_i \% 2 = 0\}\) and \(O = \{a_i \mid a_i \% 2 \neq 0\}\). The answer is the sequence \(E\) concatenated with \(O\) while preserving order.

inputFormat

The first number is n, representing the number of integers. The following n integers are the elements of the list, separated by spaces.

outputFormat

Output the rearranged list in a single line. The integers must be separated by a single space.

## sample
4 3 1 2 4
2 4 3 1