Given an array arr[], return all the elements which are less than a given value x. The order of returned smaller elements should be according their appearance in the input array.
Examples:
Input: arr[] = [54, 43, 2, 1, 5], x = 7
Output: 2 1 5
Explanation: 2, 1, 5 are smaller than 7.Input: arr[] = [324, 5, 2, 2, 3], x = 3
Output: 2 2
Explanation: 2, 2 are smaller than 3.
Try It Yourself
Single Traversal - O(n) Time
The idea is to use a single traversal to find all elements smaller than target and store them directly in the result.
Working of Approach:
- Create an empty vector result to store the required elements.
- Traverse every element of the array once.
- If the current element is smaller than target, add it to result.
- Since elements are processed from left to right, their order is preserved.
- Return the result vector after the traversal.
Let us understand with an example:
Input: arr[] = [324, 5, 2, 2, 3], x = 3
- Start with an empty res. Check 324: since 324 > 3, it is skipped.
- Check 5: since 5 > 3, it is skipped.
- Check 2: since 2 < 3, add 2 to res.
- Check the next 2: since 2 < 3, add 2 to res.
- Check 3: since 3 is not less than 3, it is skipped. Final res = [2, 2].
#include <iostream>
#include <vector>
using namespace std;
vector<int> getSmaller(vector<int> &arr, int target)
{
vector<int> res;
// Traverse the array and collect elements smaller than target.
for (int num : arr)
{
// Check if the current element is smaller than target.
if (num < target)
{
// Add the smaller element to the result.
res.push_back(num);
}
}
// Return the result.
return res;
}
int main()
{
vector<int> arr = {324, 5, 2, 2, 3};
int target = 3;
vector<int> result = getSmaller(arr, target);
for (int num : result)
{
cout << num << " ";
}
return 0;
}
import java.util.*;
class GFG {
public static ArrayList<Integer> getSmaller(int[] arr,
int target)
{
ArrayList<Integer> res = new ArrayList<>();
// Traverse the array and collect elements smaller
// than target.
for (int num : arr) {
// Check if the current element is smaller than
// target.
if (num < target) {
// Add the smaller element to the result.
res.add(num);
}
}
// Return the result.
return res;
}
public static void main(String[] args)
{
int[] arr = { 324, 5, 2, 2, 3 };
int target = 3;
ArrayList<Integer> result = getSmaller(arr, target);
// Print the elements smaller than target.
for (int num : result) {
System.out.print(num + " ");
}
}
}
def getSmaller(arr, target):
res = []
# Traverse the array and collect elements smaller than target.
for num in arr:
# Check if the current element is smaller than target.
if num < target:
# Add the smaller element to the result.
res.append(num)
# Return the result.
return res
if __name__ == "__main__":
arr = [324, 5, 2, 2, 3]
target = 3
result = getSmaller(arr, target)
for num in result:
print(num, end=' ')
using System;
using System.Collections.Generic;
public class GFG {
public static List<int> getSmaller(int[] arr,
int target)
{
List<int> res = new List<int>();
// Traverse the array and collect elements smaller
// than target.
foreach(int num in arr)
{
// Check if the current element is smaller than
// target.
if (num < target) {
// Add the smaller element to the result.
res.Add(num);
}
}
// Return the result.
return res;
}
public static void Main()
{
int[] arr = { 324, 5, 2, 2, 3 };
int target = 3;
List<int> result = getSmaller(arr, target);
foreach(int num in result)
{
Console.Write(num + " ");
}
}
}
function getSmaller(arr, target)
{
let res = [];
// Traverse the array and collect elements smaller than
// target.
for (let num of arr) {
// Check if the current element is smaller than
// target.
if (num < target) {
// Add the smaller element to the result.
res.push(num);
}
}
// Return the result.
return res;
}
// Driver Code
let arr = [ 324, 5, 2, 2, 3 ];
let target = 3;
let result = getSmaller(arr, target);
for (let num of result) {
process.stdout.write(num + " ");
}
Output
2 2