P14324 [GeeksforGeeks] K largest elements3736
普及-
题目描述

Given an array of N positive integers, print k largest elements from the array.
Example 1:
Input:
N = 5, k = 2
arr[] = {12,5,787,1,23}
Output: 787 23
Explanation: First largest element in
the array is 787 and the second largest
is 23.
Example 2:
Input:
N = 7, k = 3
arr[] = {1,23,12,9,30,2,50}
Output: 50 30 23
Explanation: Three Largest element in
the array are 50, 30 and 23.
Your Task:
Complete the function kLargest() that takes the array, N and K as input parameters and returns a list of k largest element in descending order.
Expected Time Complexity: O(N log K)
Expected Auxiliary Space: O(K)
Constraints:
1 ≤ N ≤ 10^{4}
K ≤ N
1 ≤ array[i] ≤ 10^{5}

输入格式

见题面

输出格式

见题面

样例输入
N = 5, k = 2 arr[] = {12,5,787,1,23}
样例输出
787 23