#C7021. Top K Frequent Elements
Top K Frequent Elements
Top K Frequent Elements
Given an array of integers, return the top k frequent elements in the array. In case of ties in frequency, the larger element should come first.
The frequency of an element \(a\) is defined as \(\mathrm{freq}(a)=\) the number of times it appears. The ordering is determined first by frequency in descending order and then by the element value in descending order.
inputFormat
The first line contains an integer (n) representing the number of elements in the array. The second line contains (n) space-separated integers representing the array elements. The third line contains an integer (k), the number of top frequent elements to output.
outputFormat
Output the top (k) frequent elements in order separated by spaces.## sample
6
1 1 1 2 2 3
2
1 2