2Brute2Best
🔥0

Kth Largest Element in an Array

Medium·LeetCode #215 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given an integer array nums and an integer k. Return the kth largest element in the array.

This means the kth largest in sorted order, counting from the top. It is not the kth distinct value: a value that appears twice takes two positions.

Input

nums, an integer array, and k.

Output

One integer: the kth largest value in nums.

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

Sorted from biggest: 6, 5, 4, 3, 2, 1. The 2nd value is 5.

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

Sorted from biggest: 6, 5, 5, 4, 3, 3, 2, 2, 1. The 4th value is 4. The two 5s and the two 3s each take their own position.

Constraints
  • • 1 ≤ k ≤ nums.length ≤ 10^5
  • • −10^4 ≤ nums[i] ≤ 10^4
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“kth largest element”
→ The answer is one position in sorted order. We only need a heap that holds k values, not a full order.
“k is smaller than n”
→ A heap of size k is much smaller than the array, so each step is cheap (log k).
“not the kth distinct value”
→ Each copy takes its own slot in the heap, so equal values count separately.
03

Approach Complexity Ladder

Brute Force — sort, then read index k − 1O(N log N)O(1)

Sort from biggest to smallest and read position k − 1

Better — max-heap of all values, pop k − 1 timesO(N + k log N)O(N)

Build a max-heap of all values in O(n); pop the k − 1 biggest; the top is the answer

Optimal — min-heap of size kO(N log K)O(K)

Keep the k largest seen so far in a min-heap; its top is the kth largest

Brute Force Intuition

Sort the array from biggest to smallest. Index 0 is the largest, so the kth largest is at index k − 1. Duplicates keep their own slots, so no special handling is needed.

Recognition rule for this sub-pattern:

For K largest keep a min-heap of size K; the top is the Kth largest.
Remember this · Heap — Top K
Trigger
"K largest / smallest / most frequent / closest", kth element, streaming top-K.
Think
Do I really need everything sorted, or only the best K?
Key question
Min-heap or max-heap of size K — which one lets me kick out the worst candidate?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Heap — Top K translate into production architectures and low-level memory mechanics.