Showing:
Handle Score
@Ibrahim Nash 5761
@akhayrutdinov 5111
@mb1973 4989
@Quandray 4944
@saiujwal13083 4506
@sanjay05 3762
@marius_valentin_dragoi 3516
@sushant_a 3459
@verma_ji 3341
@KshamaGupta 3318
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

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