19. Top K Frequent Elements
MediumHash MapSortingHeap
Print the k most frequent values. Break ties by the smaller value first. Print them from most to least frequent.
Input format
Line 1: n k Line 2: n integers
Output format
k space-separated values.
Example 1
Input
6 2 1 1 1 2 2 3
Output
1 2
Example 2
Input
1 1 1
Output
1
Constraints
1 ≤ k ≤ number of distinct values ≤ n ≤ 10⁵
Read input from standard input and print the answer to standard output. Your code is judged against 4 test cases, most of them hidden.
Loading the MyLoginn IDE…