Dsa
Heap
Easy
K-Largest Elements
You are tasked with identifying the K largest elements from an array. Write a program that reads a list of integers and an integer K, and outputs the K largest numbers in descending order. If there are fewer than K unique numbers, return all of them in descending order. Input is given as a single line followed by the integer K in the next line.
Example:
Input:
4 1 3 5 2 2 3 6
3
Output:
6 5 4
Key concepts
heappriority queueselection
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now