题目描述
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 ≤ 10^{6}
1 ≤ A[i] ≤ 10^{5}
输入格式
见题面
输出格式
见题面