Kth Largest / Smallest
Learn Kth Largest / Smallest (Heap, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
Finding the Kth Largest Element with a Min-Heap Keep a min-heap of size K as you scan the array; whenever it grows past K, evict the minimum. Whatever remains after the full scan has the root as the Kth largest - O(n log k), much better than sorting the whole array when k is small.