#C13377. Top K Frequent Elements
Top K Frequent Elements
Top K Frequent Elements
You are given a list of integers and a positive integer \(k\). Your task is to find the \(k\) most frequent elements in the list. In case two elements have the same frequency, the smaller element should be given priority.
The order of the output should be such that the elements with higher frequency come first. If multiple elements share the same frequency, list them in increasing order.
Note: The input is provided via standard input (stdin) and the output should be printed to standard output (stdout). The first line of input contains two integers \(n\) and \(k\), where \(n\) is the number of elements and \(k\) is the number of top frequent elements to output. The second line contains \(n\) space-separated integers.
For example, if the input is:
7 2 4 1 -1 2 -1 2 3
Then the output should be:
-1 2
inputFormat
The first line contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 10^5\)). The second line contains \(n\) space separated integers that can be positive, negative or zero.
outputFormat
Output the \(k\) most frequent elements in a single line separated by a space. The order must be such that elements with higher frequency appear first; if frequencies are equal, then the smaller element appears first.
## sample7 2
4 1 -1 2 -1 2 3
-1 2