Leaderboard
Showing:
Print K smallest elements in their original order
Basic Accuracy: 9.08% Submissions: 22 Points: 1

Given an array, the task is to print K smallest elements from the array but they must be in the same order as they are in a given array.

Example 1:
 

Input : A[] = {1, 2, 2, 3, 1} and K = 2
Output : 1 1
Explanation:
In an array A[] = {1, 2, 2, 3, 1} the smallest element is 1
and the second smallest element is also 1. So, we will return
[1, 1] as an answer.

Example 2:

Input : A[] = {1, 4, 3, 3, 5, 5} and K = 2
Output : 1 3

 

Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function kSmallestElements() that takes an array (arr), sizeOfArray (n), element (K) and return the array of K smallest element. The driver code takes care of the printing.

Expected Time Complexity: O(N*LOG(N)).
Expected Auxiliary Space: O(N).


 

Constraints:
1 ≤ K ≤ N ≤ 106
1 ≤ A[i] ≤ 105

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Print K smallest elements in their original order

Output Window