Print n smallest elements from given array in their original order
Last Updated :
27 Jul, 2022
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.
Make copy_arr[]sort(copy_arr)For all elements in arr[] - Find arr[i] in n-smallest element of copy_arr If found then print the element
Below is the implementation of above approach :
C++
// CPP for printing smallest n number in order
#include <bits/stdc++.h>
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;
}
Java
// Java for printing smallest n number in order
import java.util.*;
class GFG
{
// Function to print smallest n numbers
static void printSmall(int arr[], int asize, int n)
{
// Make copy of array
int []copy_arr = Arrays.copyOf(arr,asize);
// Sort copy array
Arrays.sort(copy_arr);
// For each arr[i] find whether
// it is a part of n-smallest
// with binary search
for (int i = 0; i < asize; ++i)
{
if (Arrays.binarySearch(copy_arr,0,n, arr[i])>-1)
System.out.print(arr[i] + " ");
}
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int asize = arr.length;
int n = 5;
printSmall(arr, asize, n);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 for printing smallest n number in order
# Function for binary_search
def binary_search(arr, low, high, ele):
while low < high:
mid = (low + high) // 2
if arr[mid] == ele:
return mid
elif arr[mid] > ele:
high = mid
else:
low = mid + 1
return -1
# Function to print smallest n numbers
def printSmall(arr, asize, n):
# Make copy of array
copy_arr = arr.copy()
# Sort copy array
copy_arr.sort()
# For each arr[i] find whether
# it is a part of n-smallest
# with binary search
for i in range(asize):
if binary_search(copy_arr, low = 0,
high = n, ele = arr[i]) > -1:
print(arr[i], end = " ")
# Driver Code
if __name__ == "__main__":
arr = [1, 5, 8, 9, 6, 7, 3, 4, 2, 0]
asize = len(arr)
n = 5
printSmall(arr, asize, n)
# This code is contributed by
# sanjeev2552
C#
// C# for printing smallest n number in order
using System;
class GFG
{
// Function to print smallest n numbers
static void printSmall(int []arr, int asize, int n)
{
// Make copy of array
int []copy_arr = new int[asize];
Array.Copy(arr, copy_arr, asize);
// Sort copy array
Array.Sort(copy_arr);
// For each arr[i] find whether
// it is a part of n-smallest
// with binary search
for (int i = 0; i < asize; ++i)
{
if (Array.BinarySearch(copy_arr, 0, n, arr[i])>-1)
Console.Write(arr[i] + " ");
}
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int asize = arr.Length;
int n = 5;
printSmall(arr, asize, n);
}
}
// This code has been contributed by 29AjayKumar
JavaScript
<script>
// Javascript for printing smallest n number in order
// Function to print smallest n numbers
function printSmall(arr, asize, n)
{
// Make copy of array
let copy_arr = [...arr];
// Sort copy array
copy_arr.sort((a, b) => a - b);
// For each arr[i] find whether
// it is a part of n-smallest
// with binary search
for (let i = 0; i < asize; ++i) {
if (arr[i] < copy_arr[n])
document.write(arr[i] + " ");
}
}
// Driver program
let arr = [1, 5, 8, 9, 6, 7, 3, 4, 2, 0];
let asize = arr.length;
let n = 5;
printSmall(arr, asize, n);
// This code is contributed by gfgking.
</script>
Time Complexity: O(n * log(n))
Auxiliary Space: O(n)
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).
Similar Reads
Sort the given Array in Spiral manner starting from the center Given an array arr[] of size N, the task is to sort the array in descending order starting from the mid of the array having the next largest element at the right of the middle element and the next largest at the left of the middle element and so on. Examples: Input: arr[] = {4, 9, 3, 5, 7}Output: 3
9 min read
Find the smallest and second smallest elements in an array Given an array arr[] of integers, find the smallest and second smallest distinct elements in the array. The result should be returned in ascending order, meaning the smallest element should come first, followed by the second smallest. If there is no valid second smallest (i.e., all elements are the
13 min read
Find the smallest and second smallest elements in an array Given an array arr[] of integers, find the smallest and second smallest distinct elements in the array. The result should be returned in ascending order, meaning the smallest element should come first, followed by the second smallest. If there is no valid second smallest (i.e., all elements are the
13 min read
Smallest greater elements in whole array An array is given of n length, and we need to calculate the next greater element for each element in the given array. If the next greater element is not available in the given array then we need to fill '_' at that index place. Examples : Input : 6 3 9 8 10 2 1 15 7 Output : 7 6 10 9 15 3 2 _ 8 Here
11 min read
Smallest greater elements in whole array An array is given of n length, and we need to calculate the next greater element for each element in the given array. If the next greater element is not available in the given array then we need to fill '_' at that index place. Examples : Input : 6 3 9 8 10 2 1 15 7 Output : 7 6 10 9 15 3 2 _ 8 Here
11 min read
Find k smallest elements in an array Given an array arr[] and an integer k, the task is to find k smallest elements in the given array. Elements in the output array can be in any order.Examples:Input: arr[] = [1, 23, 12, 9, 30, 2, 50], k = 3Output: [1, 2, 9]Input: arr[] = [11, 5, 12, 9, 44, 17, 2], k = 2Output: [2, 5]Table of Content[A
15 min read