#K78167. Top K Frequent Elements

    ID: 35027 Type: Default 1000ms 256MiB

Top K Frequent Elements

Top K Frequent Elements

You are given a list of integers and an integer \( k \). Your task is to find the \( k \) most frequent elements in the list. In case two numbers have the same frequency, the smaller number should appear first.

Input Format:

  • The first line contains two integers \( n \) and \( k \) where \( n \) is the number of elements in the list.
  • The second line contains \( n \) integers separated by spaces.

Output Format:

  • Print the \( k \) most frequent elements, separated by a single space, in one line. The ordering is determined first by descending frequency and then by ascending numerical value if frequencies are equal.

Make sure your program reads from standard input and writes to standard output.

inputFormat

The input consists of two lines:

  1. The first line contains two integers \( n \) (the number of elements in the list) and \( k \) (the number of most frequent elements to output).
  2. The second line contains \( n \) space-separated integers.

outputFormat

Output a single line containing \( k \) integers separated by a space, representing the \( k \) most frequent elements sorted by their frequency (from highest to lowest). In case of a tie, the smaller number should be printed first.

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

</p>