Created
July 27, 2017 06:42
-
-
Save 0001vrn/d77a0f96190923e1fbbf8e8858042483 to your computer and use it in GitHub Desktop.
K Largest Elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| K Largest Elements | |
| Given an array, print k largest elements from the array. | |
| The output elements should be printed in decreasing order. | |
| By : Varun Thakur | |
| Dated : 27/07/2017 | |
| */ | |
| #include <bits/stdc++.h> | |
| using namespace std; | |
| int main() { | |
| //code | |
| int t;cin>>t; | |
| while(t--) | |
| { | |
| int n,k;cin>>n>>k; | |
| int el; | |
| priority_queue<int> pq; | |
| for(int i=0;i<n;i++) | |
| { | |
| cin>>el; | |
| pq.push(el); | |
| } | |
| while(k--) | |
| { | |
| cout<< pq.top() <<' '; | |
| pq.pop(); | |
| } | |
| cout<<'\n'; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment