K Largest Elements Problem Statement
Given an unsorted array containing 'N' integers, you are required to find 'K' largest elements from the array and return them in non-decreasing order.
Input:
The first line contains an integer 'T', representing the number of test cases. For each test case, the first line contains two space-separated positive integers 'N' and 'K'. The second line contains 'N' space-separated integers representing the array elements.
Output:
For each test case, output a single line with the 'K' largest elements in non-decreasing order. Each test case is followed by a separate line.
Example:
Input:
T = 2
N, K = 5, 2
Array = [3, 5, 2, 9, 6]
N, K = 4, 3
Array = [1, 4, 2, 7]
Output:
6 9
2 4 7
Constraints:
- 1 <= T <= 100
- 1 <= N <= 104
- 1 <= K <= N
- -109 <= ARR[i] <= 109
Note:
You do not need to print anything; it has already been taken care of. Simply implement the function.

AnswerBot
4mo
Find K largest elements in an unsorted array and return them in non-decreasing order.
Sort the array in non-decreasing order.
Return the last K elements of the sorted array.
Help your peers!
Add answer anonymously...
PayPal Software Developer interview questions & answers
A Software Developer was asked 7mo agoQ. Given a list and a series of queries, how would you efficiently sort the list af...read more
A Software Developer was asked Q. Given an array of numbers, find a subset of numbers that sum to zero.
A Software Developer was asked Q. Explain how BFS works.
Popular interview questions of Software Developer
A Software Developer was asked 8mo agoQ1. Given a list and a series of queries, how would you efficiently sort the list af...read more
A Software Developer was asked Q2. Given an array of numbers, find a subset of numbers that sum to zero.
A Software Developer was asked Q3. Explain how BFS works.
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

