K-th largest element

Find the k-th largest value without sorting everything, by keeping only the k biggest values seen so far.

Time O(n log k)Space O(k)Code C#, Java, C++ and Python
Watch it animatedStep through it, with the running line highlighted. Free, no sign-in needed to watch.

The idea

Keep a min-heap of size k. Whenever it grows to k + 1, throw out the smallest. What survives is the k largest values, and the smallest of those is the answer.

Code

public static int KthLargest(int[] a, int k) {
    var heap = new PriorityQueue<int, int>(); // min-heap
    foreach (int x in a) {
        heap.Enqueue(x, x);
        if (heap.Count > k) heap.Dequeue();
    }
    return heap.Peek();
}
static int kthLargest(int[] a, int k) {
    PriorityQueue<Integer> heap = new PriorityQueue<>(); // min-heap
    for (int x : a) {
        heap.offer(x);
        if (heap.size() > k) heap.poll();
    }
    return heap.peek();
}
int kthLargest(const vector<int>& a, int k) {
    priority_queue<int, vector<int>, greater<int>> heap; // min-heap
    for (int x : a) {
        heap.push(x);
        if ((int)heap.size() > k) heap.pop();
    }
    return heap.top();
}
import heapq

def kth_largest(a: list[int], k: int) -> int:
    heap = []  # min-heap
    for x in a:
        heapq.heappush(heap, x)
        if len(heap) > k: heapq.heappop(heap)
    return heap[0]

Tips

In the .NET library

PriorityQueue<TElement, TPriority> dequeues the lowest priority first. Pass -x as the priority, or a custom IComparer, to get a max-heap.