Sort by Parity CountEasy

Sort by Parity Count

EasyPractice library · +10 XPsortingcustom sorting

Given a list of N integers, sort them in non-decreasing order based on the number of odd digits each integer contains. If two numbers have the same count of odd digits, their relative order should be preserved from the original input (stable sort). Print the sorted list.

Input

The first line contains an integer N. The second line contains N space-separated integers.

Output

Print the N space-separated integers sorted according to the rule.

Constraints

1 <= N <= 1000. Each integer will be between -1000 and 1000.

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

Example 1

Input
5
123 45 6 789 10
Output
6 45 10 123 789

Odd digit counts: 123(2), 45(2), 6(0), 789(3), 10(1). Sorted by count: 6(0), 10(1), 123(2), 45(2), 789(3). 123 and 45 maintain original relative order.

Example 2

Input
3
24 86 135
Output
24 86 135

Odd digit counts: 24(0), 86(0), 135(2). Sorted by count: 24(0), 86(0), 135(2). 24 and 86 maintain original relative order.

Hints