Sort by Digit GroupMedium

Sort by Digit Group

MediumPractice library · +20 XPsortingcustom sortingcounting sort

Given an array of N positive integers, sort the array based on the most significant digit (MSD) of each number in ascending order. If two numbers have the same MSD, their relative order does not matter. After sorting, print the count of numbers that share the same MSD. The output should be a list of counts, where the first count corresponds to numbers with MSD 1, the second to MSD 2, and so on, up to MSD 9.

Input

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

Output

Print N space-separated integers. The i-th integer is the count of numbers in the input array whose most significant digit is i. If no numbers have a particular MSD, print 0 for that MSD.

Constraints

1 <= N <= 200000. Each integer is between 1 and 10^9.

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

Example 1

Input
7
15 23 199 31 298 300 99
Output
2 2 2 0 0 0 0 0 1

Numbers sorted by MSD: [15, 199] (MSD 1), [23, 298] (MSD 2), [31, 300] (MSD 3), [99] (MSD 9). Counts: 2 for MSD 1, 2 for MSD 2, 2 for MSD 3, 0 for MSDs 4-8, 1 for MSD 9.

Example 2

Input
5
77 7 81 888 707
Output
0 0 0 0 0 0 3 2 0

Numbers sorted by MSD: [77, 7, 707] (MSD 7), [81, 888] (MSD 8). Counts: 0 for MSDs 1-6, 3 for MSD 7, 2 for MSD 8, 0 for MSD 9.

Hints