K Largest Elements Problem Statement

You are given an unsorted array containing 'N' integers. Your task is to find the 'K' largest elements from this array and return them in non-decreasing order.

Input:

The first line contains an integer 'T' which represents the number of test cases.
Each test case begins with a line containing two space-separated positive integers 'N' and 'K', where 'N' is the number of elements in the array and 'K' is the number of largest elements you need to return.
The following line contains 'N' space-separated integers representing the elements of the array.

Output:

For each test case, output 'K' largest elements from the array in non-decreasing order on a new line.

Example:

Input:
2
5 3
1 2 3 4 5
7 2
3 -2 7 1 5 0 6
Output:
3 4 5
6 7

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 104
  • 1 ≤ K ≤ N
  • -109 ≤ ARR[i] ≤ 109

Note:

You do not need to print anything; simply implement the function to return the result.

Follow Up:

Consider if you can solve this in less than O(N*log(N)) time complexity?
Be the first one to answer
Add answer anonymously...
Josh Technology Group Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter