K largest elements

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <queue> using namespace std; int main() { int arr[] = {3, 1, 5, 12, 2, 11}; int n = sizeof(arr)/sizeof(arr[0]); int k = 3; priority_queue<int , vector<int> , greater<int>> minh; for(int i=0 ; i<n; i++){ if(minh.size()==k){ minh.push(arr[i]); minh.pop(); continue; } minh.push(arr[i]); } while(minh.size()>0){ cout<<minh.top()<<" "; minh.pop(); } }
Editor Settings
Theme
Key bindings
Full width
Lines