1.Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Solution : (using Max Heap)

class Solution
{
public:
    int findKthLargest(vector<int> &nums, int k)
    {

       make_heap(nums.begin(),nums.end());

        int j = 0;
        while (j < k - 1)
        {
            pop_heap(nums.begin(),nums.end());
            nums.pop_back(); 
            j++;
        }

        return nums.front();
    }
};

Time Complexity: O(n + k log(n) ) Heapify takes O(n) and deletion of k elements takes k * log (n)

Solution: (Using Priority Queue)

Solution: (Quick Select)

Similar algorithm to quick sort

Time Complexity: O(n)

Last updated

Was this helpful?