Print the k most frequent values. Break ties by the smaller value first.
Input
- Line 1: n (
1 ≤ n ≤ 10^5)
- Line 2: n integers
- Line 3: k (
1 ≤ k ≤ number of distinct values)
Output
The k values, most frequent first, separated by single spaces.
Hint
Count with a hash map, then order by (-count, value). A heap of size k is the O(n log k) upgrade interviewers like to hear about.
Read from standard input and write to standard output. 5 test cases in total (3 hidden). Tests for this language run inside your browser.