All problems

Problem 316

Segregate Even and Odd Numbers

Easy · 10 pts · Array, Two Pointers

Problem

Rearrange the array so that all even numbers come first, followed by all odd numbers. The relative order must be kept inside each group: the even numbers appear in the same order as in the input, and so do the odd numbers. Note that negative numbers can be even or odd too (for example, -4 is even and -3 is odd).

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print the rearranged array, separated by spaces.

Constraints

1 <= N <= 100000; -1000000000 <= A[i] <= 1000000000

Example input

7
12 34 45 9 8 90 3

Example output

12 34 8 90 45 9 3

Explanation

The even numbers of 12 34 45 9 8 90 3 are 12 34 8 90 and the odd ones are 45 9 3, giving 12 34 8 90 45 9 3.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Maximum Weighted Sum over Rotations Next: Subarray with Given Sum