Sort Even and Odd NumbersEasy

Sort Even and Odd Numbers

EasyPractice library · +10 XPsortingbasic

Given a list of N integers, print all the even numbers first, followed by all the odd numbers. Within the even numbers, they should be printed in ascending order. Within the odd numbers, they should also be printed in ascending order. The original input order of elements does not matter for the output, only their parity and magnitude.

Input

The first line contains an integer N (1 <= N <= 1000). The second line contains N space-separated integers.

Output

Print N space-separated integers. The first part of the output should be all even numbers from the input, sorted in ascending order. The second part should be all odd numbers from the input, also sorted in ascending order.

Constraints

1 <= N <= 1000. Each integer is between -1000 and 1000.

Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests

Example 1

Input
5
1 2 3 4 5
Output
2 4 1 3 5

Even numbers (2, 4) sorted, then odd numbers (1, 3, 5) sorted.

Example 2

Input
7
-2 5 0 8 -1 3 6
Output
-2 0 6 8 -1 3 5

Even numbers (-2, 0, 6, 8) sorted, then odd numbers (-1, 3, 5) sorted.

Hints