You are given an array of n integers and an integer k. Report the k largest values of the array, listed from largest to smallest.
Values may repeat. Each array position counts on its own, so if the same number appears several times it may appear several times in the answer. Because the result is required to be sorted in non-increasing order, the output is uniquely determined.
Line 1: two space-separated integers n and k.
Line 2: n space-separated integers, the array values.
A single line with the k largest values in descending (non-increasing) order, separated by single spaces.
Example 1
Input
6 3 7 2 9 4 9 1
Expected
9 9 7
Explanation
Sorted descending the values are 9, 9, 7, 4, 2, 1. The three largest are 9, 9, 7 (the 9 appears twice, so it is listed twice).
Example 2
Input
4 4 -5 -1 -3 -2
Expected
-1 -2 -3 -5
Explanation
With k equal to n every value is reported, largest first: -1, -2, -3, -5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →