# Print n smallest elements from given array in their original order

We are given an array of m-elements, we need to find n smallest elements from the array but they must be in the same order as they are in given array.

Examples:

Input : arr[] = {4, 2, 6, 1, 5}, n = 3 Output : 4 2 1 Explanation : 1, 2 and 4 are 3 smallest numbers and 4 2 1 is their order in given array. Input : arr[] = {4, 12, 16, 21, 25}, n = 3 Output : 4 12 16 Explanation : 4, 12 and 16 are 3 smallest numbers and 4 12 16 is their order in given array.

Make a copy of original array and then sort copy array. After sorting the copy array, save all n smallest numbers. Further for each element in original array, check whether it is in n-smallest number or not if it present in n-smallest array then print it otherwise move forward.

- Find arr[i] in n-smallest element of copy_arr
- If found then print the element

Below is CPP implementation of above approach :

`// CPP for printing smallest n number in order ` `#include <algorithm> ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to print smallest n numbers ` `void` `printSmall(` `int` `arr[], ` `int` `asize, ` `int` `n) ` `{ ` ` ` `// Make copy of array ` ` ` `vector<` `int` `> copy_arr(arr, arr + asize); ` ` ` ` ` `// Sort copy array ` ` ` `sort(copy_arr.begin(), copy_arr.begin() + asize); ` ` ` ` ` `// For each arr[i] find whether ` ` ` `// it is a part of n-smallest ` ` ` `// with binary search ` ` ` `for` `(` `int` `i = 0; i < asize; ++i) ` ` ` `if` `(binary_search(copy_arr.begin(), ` ` ` `copy_arr.begin() + n, arr[i])) ` ` ` `cout << arr[i] << ` `" "` `; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; ` ` ` `int` `asize = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `n = 5; ` ` ` `printSmall(arr, asize, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output :

1 3 4 2 0

For making a copy of array we need space complexity of O(n) and then for sorting we will need complexity of order O(n log n). Further for each element in arr[] we are performing searching in copy_arr[], which will result O(n) for linear search but we can improve it by applying binary search and hence our overall time complexity will be** O(n log n)**.

## Recommended Posts:

- Find k maximum elements of array in original order
- Print the last occurrence of elements in array in relative order
- Print array elements in alternatively increasing and decreasing order
- Find original array from encrypted array (An array of sums of other elements)
- Generate original array from an array that store the counts of greater elements on right
- Generate original array from difference between every two consecutive elements
- Count subarrays having total distinct elements same as original array
- Find smallest subarray that contains all elements in same order
- k smallest elements in same order using O(1) extra space
- Find the smallest and second smallest elements in an array
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Average of remaining elements after removing K largest and K smallest elements from array
- Print array of strings in sorted order without copying one string into another
- Smallest greater elements in whole array
- Rearrange all elements of array which are multiples of x in increasing order

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.