Kth largest element
The first line of input contains an integer, that denotes the value of the size of the array. Let us denote it with the symbol N.
The following line contains N space separated integers, that denote the value of the elements of the array.
The following contains an integer, that denotes the value of k.
The first and only line of output contains the kth largest element
1 <= N, Ai, k <= 10^5
Time Limit: 1 sec
6
9 4 8 7 11 3
2
9
8
2 6 10 11 13 4 1 20
4
10
#include<bits/stdc++.h>int kthLargest(int* arr, int n, int k) { // Write your code here priority_queue<int , vector<int>>minh; for(int i=0;i<n;i++){ minh.push(arr[i]); }
while(k-- !=1){ minh.pop(); } return minh.top();}
Comments
Post a Comment