A diagnostic tool emits fault codes. Rebuild the stream so that codes appearing more often come first. Codes with the same frequency are ordered by ascending code value. Within a code's block, the code is simply repeated as many times as it occurred. Print the resulting sequence.
Line 1: an integer n.
Line 2: n space-separated integers, the fault codes.
A single line of n space-separated integers: the codes reordered by descending frequency, ties broken by ascending code value, each code repeated according to its count.
Example 1
Input
5 2 3 2 4 2
Expected
2 2 2 3 4
Explanation
Code 2 occurs 3 times, codes 3 and 4 once each. The output leads with 2 2 2, then the singletons 3 and 4 in ascending order.
Example 2
Input
4 5 5 1 1
Expected
1 1 5 5
Explanation
Both codes occur twice, so the tie is broken by value: 1 1 comes before 5 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →