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.
Be the first one to answer
Add answer anonymously...
Top Hike Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Hike Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app