Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
K Largest Element
MEDIUM
10 mins
227 upvotes
Sorting
Heap
Binary Search
Arrays
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Sorting
-
-
Binary Search
-
-
Arrays
-
-
Heap
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

K Largest Element

Contributed by
Prateek_18cb
Medium
yellow-spark
0/80
Avg time to solve 10 mins
Success Rate 90 %
Share
227 upvotes

Problem Statement

You are given an unsorted array containing ‘N’ integers. You need to find ‘K’ largest elements from the given array. Also, you need to return the elements in non-decreasing order.

Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
4 2
3 4 2 1
5 1
2 2 3 3 1
Sample Output 1:
3 4
3
Explanation for sample input 1:
Test case 1:
If we sort the array then it will look like: [1, 2, 3, 4]. The 2 largest elements will be [3, 4].

Test case 2:
If we sort the array then it will look like: [1, 2, 2, 3, 3]. Then the largest element will be [3].
Sample Input 2:
2
5 5
0 10 1 2 2
6 2
-2 12 -1 1 20 1 
Sample Output 2:
0 1 2 2 10
12 20
Reset Code
Full screen
Auto
copy-code
Console