#C9927. K Most Frequent Elements
K Most Frequent Elements
K Most Frequent Elements
You are given a list of integers. Your task is to find the k most frequent elements in the list. The output should list these elements in order of their frequency (from high to low). If two elements have the same frequency, the smaller number should appear first.
Note: The input is given via standard input, and the output should be written to standard output.
Example:
Input:
7 3
4 1 2 2 3 3 3
Output:
3 2 1
inputFormat
The first line contains two space-separated integers: n (the number of elements) and k (the number of most frequent elements to return). The second line contains n space-separated integers representing the elements of the list.
outputFormat
Output k integers separated by spaces representing the k most frequent elements, sorted primarily by descending frequency and secondarily by ascending numerical value.
## sample7 3
4 1 2 2 3 3 3
3 2 1