Kth Largest Element in an Array
class Solution {
public:
    // nums=[3,2,1,5,6,4]
    int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int> sorted;
       
        for(int i=0;i<nums.size();i++){
            sorted.push(nums[i]); 
        }
 // priority queue will arrange elements as[6,5,4,3,2,1]
 // now find the kth largest element and return the top element of queue
        while(k>1){
            sorted.pop();
            k--;
        }
        return sorted.top();
        
    }
};
Comments (1)