K Most Frequent
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
Discussion (opens after you solve — no spoilers before)
No comments yet.