2Brute2Best
🔥0

K Largest Elements

Easy·GeeksforGeeks Pro OnlyBasic Reinforcement
GeeksforGeeks
01

Problem Statement

You are given an array arr of integers and a number k. Find the k largest values in the array.

Return them in decreasing order, biggest first.

Input

arr, an array of integers, and k.

Output

A list of the k largest values, in decreasing order.

Example 1
Input: arr = [1, 23, 12, 9, 30, 2, 50], k = 3
Output: [50, 30, 23]

The three biggest values are 50, 30 and 23. We list them from biggest to smallest.

Example 2
Input: arr = [11, 5, 12, 9, 44, 17, 2], k = 2
Output: [44, 17]

44 is the biggest value and 17 is the second biggest.

Constraints
  • • 1 ≤ k ≤ n, where n is the length of arr
  • • arr holds integers
02

Pattern Recognition Clues

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

“k largest elements”
→ Only k answers are needed. Keep just k candidates in memory, not all N values, so use a heap of size k.
“in decreasing order”
→ A min-heap pops the smallest first, so the output must be filled from the back.
“an array of N values”
→ Every value is read once. With a size-k heap, each one costs O(log k).
03

Approach Complexity Ladder

Brute Force — sort everythingO(N log N)O(N)

Sort the whole array from biggest to smallest and keep the first k

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

Build a max-heap of all N values in O(N), then pop the top k

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

Keep the k best seen so far in a min-heap; evict the smallest when it grows past k

Brute Force Intuition

Sort the array from biggest to smallest. The first k values are the answer, already in the right order. Sorting puts all N values in order, but we only use k of them.

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.