Skip to main content
Medium Heap / Priority Queue High frequency

Kth Largest Element in an Array

Open on LeetCode

Approach Summary

Maintain a min-heap of size k. Each new element replaces the min if it is larger. The heap top is the kth largest.

Full Solution & Approach

Maintain a min-heap of size k. Iterate through the array, push each element, and whenever the heap exceeds size k, pop the smallest. After the pass the heap contains exactly the k largest elements, and its root — the minimum of those k — is the k-th largest overall. The heap stays at size k, so every push/pop is O(log k) and the total is O(n log k). This beats sorting (O(n log n)) when k is small relative to n and is the standard streaming-friendly answer. The alternative, Quickselect, gives expected O(n) but has a worst case of O(n²) and is harder to get right; the heap is the safe interview answer. In Python, heapq is a min-heap directly. In JavaScript you either build a small heap class (shown) or use the bucket approach.

Each of the n elements does one heap operation costing O(log k) — O(n log k) time. The heap holds exactly k elements — O(k) space.

Solution Code

Solution

import heapq

def find_kth_largest(nums: list[int], k: int) -> int:
    heap = nums[:k]
    heapq.heapify(heap)
    for n in nums[k:]:
        if n > heap[0]:
            heapq.heapreplace(heap, n)
    return heap[0]

Edge Cases to Watch

  • k = 1 — the largest element
  • k = n — the smallest element
  • Duplicate values — they occupy separate heap slots and are counted correctly
  • All elements identical — any of them is the answer

How to Recognize This Pattern

  • Find kth largest/smallest
  • Streaming data

Complexity Analysis

Time Complexity

O(n log k)

Space Complexity

O(k)

Tags

Array Heap Sorting Quickselect

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee