-
Notifications
You must be signed in to change notification settings - Fork 2
/
k_largest_num_min_heap.cpp
70 lines (58 loc) · 1.5 KB
/
k_largest_num_min_heap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Given an array of N positive integers, print k largest elements from the array. The output elements should be printed in decreasing order.
Input:
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N and k, N is the size of array and K is the largest elements to be returned. The second line of each test case contains N input C[i].
Output:
Print the k largest element in descending order.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 100
K ≤ N
1 ≤ C[i] ≤ 1000
Example:
Input:
2
5 2
12 5 787 1 23
7 3
1 23 12 9 30 2 50
Output:
787 23
50 30 23
Explanation:
Testcase 1: 1st largest element in the array is 787 and second largest is 23.
Testcase 2: 3 Largest element in the array are 50, 30 and 23.
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
// Implement min heap
priority_queue<int, vector<int>, greater<int> > minh;
for(int i=0;i<n;i++){
minh.push(a[i]);
if(minh.size()> k) minh.pop();
}
stack<int> s;
while(!minh.empty()){
s.push(minh.top());
minh.pop();
}
// printing in descending order
while(!s.empty()){
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
}
return 0;
}