Group by PropertyMedium

Group by Property

MediumPractice library · +20 XPhash mapgroupingsorting

Given a list of N integers, group them into sublists such that all integers in a sublist share the same property. The property of an integer x is defined as x % 10 (the last digit). Print the groups in increasing order of their property (last digit). Within each group, the numbers should appear in their original relative order.

Input

The first line contains an integer N. The second line contains N integers, separated by spaces.

Output

Print N lines. Each line contains an integer. The output should consist of all numbers belonging to the group with property 0, followed by all numbers belonging to the group with property 1, and so on, up to property 9. Within each group, numbers must maintain their original relative order.

Constraints

1 <= N <= 200000 0 <= |arr[i]| <= 10^9

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

Example 1

Input
7
12 5 34 15 7 88 10
Output
10
12
34
5
15
7
88

Numbers are grouped by last digit: {0:[10], 2:[12], 4:[34], 5:[5, 15], 7:[7], 8:[88]}. Output is ordered by last digit: 10, 12, 34, 5, 15, 7, 88.

Example 2

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

Simple case with distinct last digits.

Hints