JavaMediumHashing20 min

Sort array values by decreasing frequency

Print all values ordered by decreasing occurrence count, breaking ties by ascending numeric value. Retain every occurrence. Follow the stated ordering and edge-case conventions exactly. Interviewers use this task to probe boundary handling and collection selection.

Input

n followed by n integers.

Output

Print all values ordered by decreasing occurrence count, breaking ties by ascending numeric value. Retain every occurrence.

Constraints

0 <= n <= 100000; values in [-1000000000,1000000000].

Example

Input

6 2 3 2 4 3 2

Output

2 2 2 3 3 4

Example 2 input

3 7 7 7

Output

7 7 7

This example exercises a further boundary or ordering case.

Public test cases (2)

  • Input

    6 2 3 2 4 3 2

    Expected output

    2 2 2 3 3 4
  • Input

    4 4 1 3 2

    Expected output

    1 2 3 4
Hints (2)
  1. Identify the state needed to avoid recomputing work for each element.
  2. Count with a HashMap, sort the distinct keys by the specified comparator, and expand their counts.

Target complexity: time O(n+k log k) · space O(n+k)

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All Java programs