All problems

Problem 368

Sort Array by Increasing Frequency

Easy · 10 pts · Array, Hash Table, Sorting

Problem

Sort the array so that values with a lower frequency come first, where the frequency of a value is the number of times it appears. If two values have the same frequency, the larger value comes first. All copies of the same value stay together. For example, in 1 1 2 2 2 3, the value 3 appears once, 1 twice and 2 three times, giving 3 1 1 2 2 2.

Input

First line: N. Second line: N integers.

Output

Print the sorted array, separated by spaces.

Constraints

1 <= N <= 100000; -100 <= A[i] <= 100

Example input

5
2 3 1 3 2

Example output

1 3 3 2 2

Explanation

In 2 3 1 3 2, the value 1 appears once, while 2 and 3 appear twice. 1 comes first; between 2 and 3 the larger value comes first, giving 1 3 3 2 2.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Relative Sort Array Next: Insert Interval