#C9238. Top K Frequent Elements
Top K Frequent Elements
Top K Frequent Elements
You are given an integer array ( A = [a_1, a_2, \dots, a_n] ) and an integer ( k ). Your task is to find the ( k ) most frequent elements in the array. More formally, if the frequency of an element is the number of times it occurs in the array, you need to return the set ( S ) of ( k ) elements with the highest frequencies. The answer can be returned in any order, but for the purposes of consistency in this problem, you should output the result in ascending order.
Input Format: The first line contains two integers ( n ) (the number of elements in the array) and ( k ) (the number of most frequent elements to find). The second line contains ( n ) space-separated integers representing the array ( A ).
Output Format: Output a single line containing the ( k ) most frequent elements in ascending order, separated by a space.
Constraints:
- \( 1 \leq k \leq n \leq 10^5 \)
- \( -10^9 \leq a_i \leq 10^9 \)
Example:
Input: 6 2 1 1 1 2 2 3 Output: 1 2
inputFormat
Input Description: The first input line contains two integers ( n ) and ( k ) separated by a space, where ( n ) is the number of elements in the array and ( k ) indicates the number of most frequent elements to output. The second line contains ( n ) space-separated integers representing the array ( A ).
outputFormat
Output Description: Output a single line with ( k ) integers representing the most frequent elements in the array in ascending order, separated by a space.## sample
6 2
1 1 1 2 2 3
1 2