All problems

Problem 561

Sort Array by Parity II

Easy · 10 pts · Array, Two Pointers

Problem

The array has an even length N, and exactly half of its numbers are even. Rearrange it so that every even index (0, 2, 4, ...) holds an even number and every odd index holds an odd number. To make the answer unique, the even numbers must keep their original relative order, and so must the odd numbers. Collect the evens and the odds separately, then interleave them.

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]. N is even and exactly N / 2 of the values are even.

Output

Print the rearranged array, space-separated.

Constraints

2 <= N <= 100000; N is even; 0 <= A[i] <= 1000

Example input

4
4 2 5 7

Example output

4 5 2 7

Explanation

In 4 2 5 7 the evens in order are 4 2 and the odds are 5 7. Interleaving them gives 4 5 2 7: even numbers at indices 0 and 2, odd numbers at indices 1 and 3.

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

Previous: Linear Recurrence by Matrix Power Next: Remove Covered Intervals