Skip to content

Instantly share code, notes, and snippets.

@0001vrn
Created July 27, 2017 06:42
Show Gist options
  • Select an option

  • Save 0001vrn/d77a0f96190923e1fbbf8e8858042483 to your computer and use it in GitHub Desktop.

Select an option

Save 0001vrn/d77a0f96190923e1fbbf8e8858042483 to your computer and use it in GitHub Desktop.
K Largest Elements
/*
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