#K55787. K Largest Elements

    ID: 30053 Type: Default 1000ms 256MiB

K Largest Elements

K Largest Elements

Given an array of integers and an integer ( k ), your task is to find the ( k ) largest elements and output them in descending order.

Note: The output must list the numbers from the largest to the smallest among the top ( k ) elements. Use an efficient approach such as a min-heap to achieve this, especially for large inputs.

inputFormat

The input is provided via standard input (stdin) and consists of two lines:
1. The first line contains two integers ( n ) and ( k ), where ( n ) is the number of elements in the array and ( k ) is the number of largest elements to extract.
2. The second line contains ( n ) space-separated integers representing the array elements.

outputFormat

Output the ( k ) largest elements in descending order to standard output (stdout), separated by a single space. If ( k ) is zero or the array is empty, output an empty line.## sample

6 3
4 1 7 3 9 2
9 7 4