Quick actions

cmd+k|ctrl+k

Navigation

Languages

K largest elements

Snippet info

Language

Cpp

Visibility

public

Author

amangoyal1727

Created

2021-05-29T23:51:04.670525Z

Updated

2021-05-29T23:51:04.670525Z

#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();
    }
    
    
}
INFO