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
7d

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
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