#C7102. Largest K Distinct Integers
Largest K Distinct Integers
Largest K Distinct Integers
You are given a list of N integers and an integer K. Your task is to find the K largest distinct integers from the list and output them in descending order. If there are fewer than K distinct integers, output all of them in descending order.
The problem can be formalized as follows:
Given a list \(a_1,a_2,\ldots,a_N\), find a set \(S\) (subset of unique elements) such that \(|S| = \min(K,|\{a_1,\ldots,a_N\}|)\) and if we denote the sorted set in descending order as \(s_1 \geq s_2 \geq \cdots \), then output the first \(\min(K,|\{a_1,\ldots,a_N\}|)\) numbers.
Input is taken from standard input and output is written to standard output.
inputFormat
The first line contains two integers N and K, where N is the number of elements in the list and K denotes how many largest distinct integers to pick.
The second line contains N integers separated by spaces.
If N is 0, the second line may be empty.
outputFormat
Output the K largest distinct integers in descending order in a single line, separated by a single space. If no integers exist, output an empty line.
## sample5 3
4 6 2 6 4
6 4 2