Problems > K Most Frequent

K Most Frequent

Difficulty: 1300 3500 ms 128 MB

heap sorting

Print the K most frequent values, ordered by decreasing frequency; break frequency ties by smaller value first.

Input: the first line has N and K (K never exceeds the number of distinct values); the second line has N integers.

Output: the K values in the required order.

Constraints:

  • 1 ≤ N ≤ 105
  • −109 ≤ ai ≤ 109

Sample Tests

Input 1
6 2
1 1 1 2 2 3
Output 1
1 2
Input 2
2 2
5 7
Output 2
5 7

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.