#K59552. K Most Frequent Elements

    ID: 30889 Type: Default 1000ms 256MiB

K Most Frequent Elements

K Most Frequent Elements

Given an array of n integers and an integer k, your task is to find the k most frequent elements from the array. In case of a tie (i.e. two numbers having the same frequency), choose the smaller number first.

You should output the selected k numbers in order: sorted primarily by decreasing frequency and secondarily by increasing numerical value.

The solution must read input from standard input (stdin) and output to standard output (stdout).

Note: It is guaranteed that k is less than or equal to the number of distinct elements in the array.

The frequency ordering is defined by the formula:

\( (v, f) \) where v is the value and f is its frequency, sorted by \(-f\) and then \(v\).

inputFormat

The first line contains two integers n and k separated by a space, where n is the total number of elements in the array and k is the number of most frequent elements to output.

The second line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single line containing k integers separated by a space. These integers are the k most frequent elements sorted primarily by frequency (in descending order) and then by value (in ascending order).

## sample
6 2
1 1 1 2 2 3
1 2

</p>