Problems > Evens First, Order Kept

Evens First, Order Kept

Difficulty: 1000 3000 ms 128 MB

implementation sorting

Reorder the list so every even number comes before every odd number, preserving the original relative order within each group.

Input: the first line has N; the second line has N integers.

Output: the reordered list on one line.

Constraints:

  • 1 ≤ N ≤ 105
  • −109 ≤ ai ≤ 109

Sample Tests

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

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.