#215

Kth Largest Element in an Array

Medium Min-heap / quickselect

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the k-th largest element of an unsorted array (by sorted position, so duplicates count).

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the k-th largest value in an unsorted array.

Why this works: a min-heap of size k keeps the k largest; its top is the answer.

Example: [3, 2, 1, 5, 6, 4], k = 2

heap ends as {5, 6} → top 5

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Treating it as "k-th largest distinct value" and removing duplicates.

Solve on LeetCode ↗
Complexity
  • Time: O(n log k).
  • Space: O(k).
Topic: Heaps & Priority Queue
🎬 Episodes 64–68: video coming soon