#C3359. Top K Frequent Elements
Top K Frequent Elements
Top K Frequent Elements
You are given an array of integers and an integer (k). Your task is to find the (k) most frequent elements in the array. In case several elements have the same frequency, the smaller number should come first. If (k) is greater than the number of distinct elements in the array, output all distinct elements in the desired order.
(\textbf{Input:}) The first line consists of two integers (n) and (k) (the number of elements and the number of top frequent elements, respectively). The second line contains (n) space-separated integers.
(\textbf{Output:}) Output the (k) most frequent elements sorted first by decreasing frequency and then by increasing numerical value. The output should be a single line of space-separated integers.
inputFormat
Input is read from the standard input (stdin).
The first line contains two integers (n) and (k), where (n) is the number of elements in the array and (k) is the number of frequent elements to output.
The second line contains (n) space-separated integers representing the elements of the array.
outputFormat
Print (k) most frequent elements in one line separated by a space. The elements must be sorted by descending frequency, and if frequencies are equal, by ascending numerical value.## sample
6 2
1 1 1 2 2 3
1 2