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!
Select
Add answer anonymously...

Hike Software Developer interview questions & answers

A Software Developer was asked Q. Given an array of integers representing the heights of bars in a histogram, find...read more
A Software Developer was asked Q. Statistics From A Large Sample Task Calculate various statistical measures given...read more
A Software Developer was asked Q. Pair Sum in Binary Search Tree Given a Binary Search Tree (BST) and a target val...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Given an array of integers representing the heights of bars in a histogram, find...read more
A Software Developer was asked Q2. Statistics From A Large Sample Task Calculate various statistical measures given...read more
A Software Developer was asked Q3. Pair Sum in Binary Search Tree Given a Binary Search Tree (BST) and a target val...read more
Hike Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits