#K6056. K Most Frequent Elements
K Most Frequent Elements
K Most Frequent Elements
You are given a list of n integers and an integer k. Your task is to output the k most frequent elements in the list, sorted in increasing order. When multiple elements have the same frequency, the element with the smaller value is preferred.
If k is greater than the number of unique elements, output all the distinct elements in sorted order. If the input list is empty or if k is 0, output nothing.
Input: The input is read from standard input (stdin).
Output: Print the results to standard output (stdout) as a single line of space-separated integers.
Note: The frequency of an element is the number of times it occurs in the list.
inputFormat
The first line contains two integers: n (the number of elements) and k (the number of most frequent elements to output). The second line contains n space-separated integers.
outputFormat
Print the k most frequent elements in sorted (increasing) order on a single line, separated by spaces. If there are no elements to print, output an empty line.## sample
6 2
1 1 1 2 2 3
1 2
</p>