A festival's entrance scanner reads the color code printed on every attendee's wristband as they walk through the gate, producing a list of n integer codes in arrival order (the same color can, and usually does, belong to many attendees). To plan how many replacement wristbands of each color to print for next year, organizers want the scanned list rewritten so that colors that were scanned less often over the whole day appear before colors that were scanned more often; whenever two colors were scanned exactly the same number of times, the color with the larger numeric code must appear before the one with the smaller code among that tied group. Rewrite the full list of n scanned codes according to this rule.
Line 1: a single integer n, the number of scanned wristbands. Line 2: n space-separated integers, the color code scanned for each attendee, in arrival order.
n space-separated integers: the codes reordered by ascending scan frequency, with ties among equally-frequent colors broken by descending code value.
Example 1
Input
6 1 1 2 2 2 3
Expected
3 1 1 2 2 2
Explanation
Code 3 occurs once, code 1 occurs twice, and code 2 occurs three times. Ordered by ascending occurrence count, code 3 (count 1) comes first, then the two 1s (count 2), then the three 2s (count 3), giving the output 3 1 1 2 2 2.
Example 2
Input
5 2 3 1 3 2
Expected
1 3 3 2 2
Explanation
Code 1 occurs once, while codes 2 and 3 each occur twice. Code 1 (the lowest count) comes first. Codes 2 and 3 are tied on count, so the tie is broken by descending value: 3 before 2. This gives the output 1 3 3 2 2.
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 →