Open In App

For each in 1st array count less than or equal to it in 2nd array

Last Updated : 23 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

You are given two unsorted arrays a[] and b[]. Both arrays may contain duplicate elements. For each element in a[], your task is to count how many elements in b[] are less than or equal to that element.

Examples:

Input: a[] = [1, 2, 3, 4, 7, 9], b = [0, 1, 2, 1, 1, 4]
Output: [4, 5, 5, 6, 6, 6]
Explanation:
For a[0] = 1, there are 4 elements in b (0, 1, 1, 1) that are ≤ 1.
For a[1] = 2, there are 5 elements in b (0, 1, 1, 1, 2) that are ≤ 2.
For a[2] = 3, there are 5 elements in b that are ≤ 3.
Similarly, for a[3] = 4, there are 6 elements in b that are ≤ 4, and for a[4] = 7 and a[5] = 9, there are also 6 elements in b that are ≤ 7 and ≤ 9, respectively.

Input: a[] = [4, 8, 7, 5, 1], b = [4, 48, 3, 0, 1, 1, 5]
Output: [5, 6, 6, 6, 3]
Explanation:
For a[0] = 4, there are 5 elements in b (4, 3, 0, 1, 1) that are ≤ 4.
For a[1] = 8 and a[2] = 7, there are 6 elements in b that are ≤ 8 and ≤ 7.
For a[3] = 5, there are 6 elements in b that are ≤ 5.
For a[4] = 1, there are 3 elements in b (0, 1, 1) that are ≤ 1.

[Naive Approach] – Using Nested Loops – O(n * m) Time and O(n) Space

The idea is to use two loops, the outer loop for elements of array a[] and an inner loop for elements of array b[]. Then for each element of a[], count elements less than or equal to it in b[].

Follow the below given step by step approach

  1. Traverse through the elements of the first array from start to end.
  2. For every element in the first array.
  3. Traverse through the elements in the second array and find the count of elements that are less than or equal to the element of the first array.
  4. Print the count for every index.

Below is given the implementation:

C++
#include <bits/stdc++.h>
using namespace std;

vector<int> countEleLessThanOrEqual
(vector<int>& a, vector<int>& b) {
    int n = a.size(), m = b.size();

    // to store the result
    vector<int> res(n);

    // outer loop for each element of a[]
    for (int i = 0; i < n; i++) {
        int count = 0;

        // inner loop for each element of b[]
        for (int j = 0; j < m; j++)
            if (b[j] <= a[i])
                count++;

        res[i] = count;
    }

    return res;
}

int main() {
    vector<int> a = {1, 2, 3, 4, 7, 9};
    vector<int> b = {0, 1, 2, 1, 1, 4};
    vector<int> result = countEleLessThanOrEqual(a, b);
    for (int i : result) {
        cout << i << " ";
    }
    return 0;
}
Java
import java.util.*;

class GfG {

    // to store the result
    public static ArrayList<Integer> 
    countEleLessThanOrEqual(int[] a, int[] b) {
        int n = a.length, m = b.length;

        // to store the result
        ArrayList<Integer> res = new ArrayList<>();

        // outer loop for each element of a[]
        for (int i = 0; i < n; i++) {
            int count = 0;

            // inner loop for each element of b[]
            for (int j = 0; j < m; j++)
                if (b[j] <= a[i])
                    count++;

            res.add(count);
        }

        return res;
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 7, 9};
        int[] b = {0, 1, 2, 1, 1, 4};
        ArrayList<Integer> result = countEleLessThanOrEqual(a, b);
        for (int i : result) {
            System.out.print(i + " ");
        }
    }
}
Python
# to store the result
def countEleLessThanOrEqual(a, b):
    n = len(a)
    m = len(b)

    # to store the result
    res = [0] * n

    # outer loop for each element of a[]
    for i in range(n):
        count = 0

        # inner loop for each element of b[]
        for j in range(m):
            if b[j] <= a[i]:
                count += 1

        res[i] = count

    return res

a = [1, 2, 3, 4, 7, 9]
b = [0, 1, 2, 1, 1, 4]
result = countEleLessThanOrEqual(a, b)
for i in result:
    print(i, end=" ")
C#
using System;
using System.Collections.Generic;

class GfG {

    // to store the result
    public static List<int> 
    countEleLessThanOrEqual(int[] a, int[] b) {
        int n = a.Length, m = b.Length;

        // to store the result
        List<int> res = new List<int>();

        // outer loop for each element of a[]
        for (int i = 0; i < n; i++) {
            int count = 0;

            // inner loop for each element of b[]
            for (int j = 0; j < m; j++)
                if (b[j] <= a[i])
                    count++;

            res.Add(count);
        }

        return res;
    }

    public static void Main(string[] args) {
        int[] a = {1, 2, 3, 4, 7, 9};
        int[] b = {0, 1, 2, 1, 1, 4};
        List<int> result = countEleLessThanOrEqual(a, b);
        foreach (int i in result) {
            Console.Write(i + " ");
        }
    }
}
JavaScript
// to store the result
function countEleLessThanOrEqual(a, b) {
    let n = a.length, m = b.length;

    // to store the result
    let res = new Array(n);

    // outer loop for each element of a[]
    for (let i = 0; i < n; i++) {
        let count = 0;

        // inner loop for each element of b[]
        for (let j = 0; j < m; j++)
            if (b[j] <= a[i])
                count++;

        res[i] = count;
    }

    return res;
}

let a = [1, 2, 3, 4, 7, 9];
let b = [0, 1, 2, 1, 1, 4];
let result = countEleLessThanOrEqual(a, b);
console.log(result.join(" "));

Output
4 5 5 6 6 6 

[Better Approach – 1] – Using Sorting – O((n + m) * log m) Time and O(n) Space

The idea is to sort the elements of array b[], then perform a modified binary search on array b[]. For each element x of array a[], find the last index of the largest element smaller than or equal to x in sorted array b[]. The index of the largest element will give the count of elements.

Below is given the step by step process:

  1. Sort the second array.
  2. Traverse through the elements of the first array from start to end.
  3. For every element in the first array.
  4. Do a binary search on the second array and find the index of the largest element smaller than or equal to the element of the first array.
  5. The index of the largest element will give the count of elements. Print the count for every index.

Below is given the implementation:

C++
#include <bits/stdc++.h>
using namespace std;

// to perform the binary search
int binarySearch(vector<int> &arr, int x) {
    int low = 0, high = arr.size() - 1;

    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] <= x)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return low;
}

vector<int> countEleLessThanOrEqual
    (vector<int>& a, vector<int>& b) {
    int n = a.size(), m = b.size();

    // to store the result
    vector<int> res(n);

    // sort the array b[]
    sort(b.begin(), b.end());

    // outer loop for each element of a[]
    for (int i = 0; i < n; i++) {
        res[i] = binarySearch(b, a[i]);
    }

    return res;
}

int main() {
    vector<int> a = {1, 2, 3, 4, 7, 9};
    vector<int> b = {0, 1, 2, 1, 1, 4};
    vector<int> result = countEleLessThanOrEqual(a, b);
    for (int i : result) {
        cout << i << " ";
    }
    return 0;
}
Java
import java.util.*;

class GfG {

    // to perform the binary search
    static int binarySearch(int[] arr, int x) {
        int low = 0, high = arr.length - 1;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (arr[mid] <= x)
                low = mid + 1;
            else
                high = mid - 1;
        }
        return low;
    }

    // to store the result
    static ArrayList<Integer> 
    countEleLessThanOrEqual(int[] a, int[] b) {
        int n = a.length, m = b.length;

        // to store the result
        ArrayList<Integer> res = new ArrayList<>();

        // sort the array b[]
        Arrays.sort(b);

        // outer loop for each element of a[]
        for (int i = 0; i < n; i++) {
            res.add(binarySearch(b, a[i]));
        }

        return res;
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 7, 9};
        int[] b = {0, 1, 2, 1, 1, 4};
        ArrayList<Integer> result = countEleLessThanOrEqual(a, b);
        for (int i : result) {
            System.out.print(i + " ");
        }
    }
}
Python
# to perform the binary search
def binarySearch(arr, x):
    low = 0
    high = len(arr) - 1

    while low <= high:
        mid = low + (high - low) // 2
        if arr[mid] <= x:
            low = mid + 1
        else:
            high = mid - 1
    return low

# to store the result
def countEleLessThanOrEqual(a, b):
    n = len(a)
    m = len(b)

    # to store the result
    res = [0] * n

    # sort the array b[]
    b.sort()

    # outer loop for each element of a[]
    for i in range(n):
        res[i] = binarySearch(b, a[i])
    return res

a = [1, 2, 3, 4, 7, 9]
b = [0, 1, 2, 1, 1, 4]
result = countEleLessThanOrEqual(a, b)
for i in result:
    print(i, end=" ")
C#
using System;
using System.Collections.Generic;

class GfG {

    // to perform the binary search
    static int binarySearch(int[] arr, int x) {
        int low = 0, high = arr.Length - 1;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (arr[mid] <= x)
                low = mid + 1;
            else
                high = mid - 1;
        }
        return low;
    }

    // to store the result
    static List<int> 
    countEleLessThanOrEqual(int[] a, int[] b) {
        int n = a.Length, m = b.Length;

        // to store the result
        List<int> res = new List<int>();

        // sort the array b[]
        Array.Sort(b);

        // outer loop for each element of a[]
        for (int i = 0; i < n; i++) {
            res.Add(binarySearch(b, a[i]));
        }

        return res;
    }

    public static void Main(string[] args) {
        int[] a = {1, 2, 3, 4, 7, 9};
        int[] b = {0, 1, 2, 1, 1, 4};
        List<int> result = countEleLessThanOrEqual(a, b);
        foreach (int i in result) {
            Console.Write(i + " ");
        }
    }
}
JavaScript
// to perform the binary search
function binarySearch(arr, x) {
    let low = 0, high = arr.length - 1;

    while (low <= high) {
        let mid = low + Math.floor((high - low) / 2);
        if (arr[mid] <= x)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return low;
}

// to store the result
function countEleLessThanOrEqual(a, b) {
    let n = a.length, m = b.length;

    // to store the result
    let res = new Array(n);

    // sort the array b[]
    b.sort((x, y) => x - y);

    // outer loop for each element of a[]
    for (let i = 0; i < n; i++) {
        res[i] = binarySearch(b, a[i]);
    }

    return res;
}

let a = [1, 2, 3, 4, 7, 9];
let b = [0, 1, 2, 1, 1, 4];
let result = countEleLessThanOrEqual(a, b);
console.log(result.join(" "));

Output
4 5 5 6 6 6 

[Better Approach – 2] – Using Inbuilt Function – O((n + m) * log m) Time and O(n) Space

Instead of implementing the binary search in the above approach, we can use inbuilt function upper bound in C++ (and bisect_right in Python) that returns the first index ind in the array where the value arr[ind] > target.

C++
#include <bits/stdc++.h>
using namespace std;

vector<int> countEleLessThanOrEqual
    (vector<int>& a, vector<int>& b) {
    int n = a.size(), m = b.size();

    // to store the result
    vector<int> res(n);

    // sort the array b[]
    sort(b.begin(), b.end());

    // outer loop for each element of a[]
    for (int i = 0; i < n; i++) {
        res[i] = upper_bound(b.begin(), 
        b.end(), a[i]) - b.begin();
    }

    return res;
}

int main() {
    vector<int> a = {1, 2, 3, 4, 7, 9};
    vector<int> b = {0, 1, 2, 1, 1, 4};
    vector<int> result = countEleLessThanOrEqual(a, b);
    for (int i : result) {
        cout << i << " ";
    }
    return 0;
}
Java
import java.util.*;

class GfG {

    // to perform upper_bound functionality
    static int upperBound(int[] arr, int x) {
        int low = 0, high = arr.length;
        while (low < high) {
            int mid = low + (high - low) / 2;
            if (arr[mid] <= x)
                low = mid + 1;
            else
                high = mid;
        }
        return low;
    }

    // to store the result
    public static ArrayList<Integer> 
    countEleLessThanOrEqual(int[] a, int[] b) {
        int n = a.length, m = b.length;

        // sort the array b[]
        Arrays.sort(b);

        // outer loop for each element of a[]
        ArrayList<Integer> res = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            res.add(upperBound(b, a[i]));
        }

        return res;
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 7, 9};
        int[] b = {0, 1, 2, 1, 1, 4};
        ArrayList<Integer> result = 
        countEleLessThanOrEqual(a, b);
        for (int i : result) {
            System.out.print(i + " ");
        }
    }
}
Python
# to store the result
def countEleLessThanOrEqual(a, b):
    n = len(a)
    m = len(b)

    # sort the array b[]
    b.sort()

    # to store the result
    res = [0] * n

    # outer loop for each element of a[]
    for i in range(n):
        # bisect_right is equivalent to upper_bound
        import bisect
        res[i] = bisect.bisect_right(b, a[i])
    return res

a = [1, 2, 3, 4, 7, 9]
b = [0, 1, 2, 1, 1, 4]
result = countEleLessThanOrEqual(a, b)
for i in result:
    print(i, end=" ")
C#
using System;
using System.Collections.Generic;

class GfG {

    // to perform upper_bound functionality
    static int UpperBound(int[] arr, int x) {
        int low = 0, high = arr.Length;
        while (low < high) {
            int mid = low + (high - low) / 2;
            if (arr[mid] <= x)
                low = mid + 1;
            else
                high = mid;
        }
        return low;
    }

    // to store the result
    public static List<int> 
    countEleLessThanOrEqual(int[] a, int[] b) {
        int n = a.Length, m = b.Length;

        // sort the array b[]
        Array.Sort(b);

        // outer loop for each element of a[]
        List<int> res = new List<int>();
        for (int i = 0; i < n; i++) {
            res.Add(UpperBound(b, a[i]));
        }

        return res;
    }

    public static void Main(string[] args) {
        int[] a = {1, 2, 3, 4, 7, 9};
        int[] b = {0, 1, 2, 1, 1, 4};
        List<int> result = countEleLessThanOrEqual(a, b);
        foreach (int i in result) {
            Console.Write(i + " ");
        }
    }
}
JavaScript
// to perform upper_bound functionality
function upperBound(arr, x) {
    let low = 0, high = arr.length;
    while (low < high) {
        let mid = low + Math.floor((high - low) / 2);
        if (arr[mid] <= x)
            low = mid + 1;
        else
            high = mid;
    }
    return low;
}

// to store the result
function countEleLessThanOrEqual(a, b) {
    let n = a.length, m = b.length;

    // sort the array b[]
    b.sort((x, y) => x - y);

    // outer loop for each element of a[]
    let res = new Array(n);
    for (let i = 0; i < n; i++) {
        res[i] = upperBound(b, a[i]);
    }

    return res;
}

let a = [1, 2, 3, 4, 7, 9];
let b = [0, 1, 2, 1, 1, 4];
let result = countEleLessThanOrEqual(a, b);
console.log(result.join(" "));

Output
4 5 5 6 6 6 

[Better Approach – 3] – Using Sorting and Two Pointers – O(n*logn + m*logm) Time and O(n) Space

The idea is to sort both the arrays and put pointers i and j for array a[] and b[] at the beginning. Then traverse over the elements of a by using i pointer & inside this loop, we will go over each element of b by j pointer. Wherever arr2[j] <= arr1[i] we will increase j . if the condition fails store j.

Note:- This method will return the value in sorted array, thus can be used for sorted arrays.

C++
#include <bits/stdc++.h>
using namespace std;

vector<int> countEleLessThanOrEqual(vector<int>& a, vector<int>& b) {
    int n = a.size(), m = b.size();

    // to store the result
    vector<int> res(n);

    // sort both the arrays
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());

    int ind = 0;

    // outer loop for each element of a[]
    for (int i = 0; i < n; i++) {
        
        // inner loop for each element of b[]
        while (ind < m && b[ind] <= a[i]) {
            ind++;
        }
        res[i] = ind;
    }

    return res;
}

int main() {
    vector<int> a = {1, 2, 3, 4, 7, 9};
    vector<int> b = {0, 1, 2, 1, 1, 4};
    vector<int> result = countEleLessThanOrEqual(a, b);
    for (int i : result) {
        cout << i << " ";
    }
    return 0;
}
Java
import java.util.*;

class GfG {

    // to store the result
    public static ArrayList<Integer> 
    countEleLessThanOrEqual(int[] a, int[] b) {
        int n = a.length, m = b.length;

        // to store the result
        ArrayList<Integer> res = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            res.add(0);
        }

        // sort both the arrays
        Arrays.sort(a);
        Arrays.sort(b);

        int ind = 0;

        // outer loop for each element of a[]
        for (int i = 0; i < n; i++) {
            
            // inner loop for each element of b[]
            while (ind < m && b[ind] <= a[i]) {
                ind++;
            }
            res.set(i, ind);
        }

        return res;
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 7, 9};
        int[] b = {0, 1, 2, 1, 1, 4};
        ArrayList<Integer> result = countEleLessThanOrEqual(a, b);
        for (int i : result) {
            System.out.print(i + " ");
        }
    }
}
Python
# to store the result
def countEleLessThanOrEqual(a, b):
    n = len(a)
    m = len(b)

    # to store the result
    res = [0] * n

    # sort both the arrays
    a.sort()
    b.sort()

    ind = 0

    # outer loop for each element of a[]
    for i in range(n):
        
        # inner loop for each element of b[]
        while ind < m and b[ind] <= a[i]:
            ind += 1
        res[i] = ind

    return res

a = [1, 2, 3, 4, 7, 9]
b = [0, 1, 2, 1, 1, 4]
result = countEleLessThanOrEqual(a, b)
for i in result:
    print(i, end=" ")
C#
using System;
using System.Collections.Generic;

class GfG {

    // to store the result
    public static List<int> 
    countEleLessThanOrEqual(int[] a, int[] b) {
        int n = a.Length, m = b.Length;

        // to store the result
        List<int> res = new List<int>();
        for (int i = 0; i < n; i++) {
            res.Add(0);
        }

        // sort both the arrays
        Array.Sort(a);
        Array.Sort(b);

        int ind = 0;

        // outer loop for each element of a[]
        for (int i = 0; i < n; i++) {
            
            // inner loop for each element of b[]
            while (ind < m && b[ind] <= a[i]) {
                ind++;
            }
            res[i] = ind;
        }

        return res;
    }

    public static void Main(string[] args) {
        int[] a = {1, 2, 3, 4, 7, 9};
        int[] b = {0, 1, 2, 1, 1, 4};
        List<int> result = countEleLessThanOrEqual(a, b);
        foreach (int i in result) {
            Console.Write(i + " ");
        }
    }
}
JavaScript
// to store the result
function countEleLessThanOrEqual(a, b) {
    let n = a.length, m = b.length;

    // to store the result
    let res = new Array(n).fill(0);

    // sort both the arrays
    a.sort((x, y) => x - y);
    b.sort((x, y) => x - y);

    let ind = 0;

    // outer loop for each element of a[]
    for (let i = 0; i < n; i++) {
        
        // inner loop for each element of b[]
        while (ind < m && b[ind] <= a[i]) {
            ind++;
        }
        res[i] = ind;
    }

    return res;
}

let a = [1, 2, 3, 4, 7, 9];
let b = [0, 1, 2, 1, 1, 4];
let result = countEleLessThanOrEqual(a, b);
console.log(result.join(" "));

Output
4 5 5 6 6 6 

[Expected Approach for Small Range] – Using Count Array and Prefix Sum – O(n + m) Time and O(n + m) Space

The idea is to create hash table cnt[] of size 10^5 to store the frequency of elements in array b[] (considering the max value in both the arrays <= 10^5). Now, create the prefix array of the hash table, then every element cnt[ind] will store the number of elements in range [0, ind]. Now for every element a[i], the value cnt[a[i]] stores the required answer.

Follow the steps to implement the approach:

  1. Create a hash table to store the frequency of elements in b[]. Initialize the hash table with zeros.
  2. Iterate through b[] and update the frequency of each element in the hash table.
  3. Create a prefix sum array cnt[] of size 100010 (maximum element value + 10). Initialize cnt[0] with the frequency of the first element in b[].
  4. Calculate the prefix sum array by adding the current frequency with the previous prefix sum.
  5. Iterate through a. For each element a[i], access cnt[a[i]] to get the count of elements less than or equal to a[i] in b.
  6. Store the counts in a result array or directly print them.

Below is given the implementation:

C++
#include <bits/stdc++.h>
using namespace std;

vector<int> countEleLessThanOrEqual
(vector<int>& a, vector<int>& b) {
    int n = a.size(), m = b.size();

    // to store the result
    vector<int> res(n);

    // to store frequency of elements in b[]
    vector<int> cnt(100001, 0);
    for (int i = 0; i < m; i++) {
        cnt[b[i]]++;
    }

    // transform cnt[] to prefix sum array
    for (int i = 1; i < 100001; i++) {
        cnt[i] += cnt[i - 1];
    }

    // loop for each element of a[]
    for (int i = 0; i < n; i++) {
        res[i] = cnt[a[i]];
    }

    return res;
}

int main() {
    vector<int> a = {1, 2, 3, 4, 7, 9};
    vector<int> b = {0, 1, 2, 1, 1, 4};
    vector<int> result = countEleLessThanOrEqual(a, b);
    for (int i : result) {
        cout << i << " ";
    }
    return 0;
}
Java
import java.util.*;

class GfG {

    // to store the result
    public static ArrayList<Integer> 
    countEleLessThanOrEqual(int[] a, int[] b) {
        int n = a.length, m = b.length;

        // to store the result
        ArrayList<Integer> res = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            res.add(0);
        }

        // to store frequency of elements in b[]
        int[] cnt = new int[100001];
        for (int i = 0; i < 100001; i++) {
            cnt[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            cnt[b[i]]++;
        }

        // transform cnt[] to prefix sum array
        for (int i = 1; i < 100001; i++) {
            cnt[i] += cnt[i - 1];
        }

        // loop for each element of a[]
        for (int i = 0; i < n; i++) {
            res.set(i, cnt[a[i]]);
        }

        return res;
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 7, 9};
        int[] b = {0, 1, 2, 1, 1, 4};
        ArrayList<Integer> result = countEleLessThanOrEqual(a, b);
        for (int i : result) {
            System.out.print(i + " ");
        }
    }
}
Python
# to store the result
def countEleLessThanOrEqual(a, b):
    n = len(a)
    m = len(b)

    # to store the result
    res = [0] * n

    # to store frequency of elements in b[]
    cnt = [0] * 100001
    for i in range(m):
        cnt[b[i]] += 1

    # transform cnt[] to prefix sum array
    for i in range(1, 100001):
        cnt[i] += cnt[i - 1]

    # loop for each element of a[]
    for i in range(n):
        res[i] = cnt[a[i]]

    return res

a = [1, 2, 3, 4, 7, 9]
b = [0, 1, 2, 1, 1, 4]
result = countEleLessThanOrEqual(a, b)
for i in result:
    print(i, end=" ")
C#
using System;
using System.Collections.Generic;

class GfG {

    // to store the result
    public static List<int> 
    countEleLessThanOrEqual(int[] a, int[] b) {
        int n = a.Length, m = b.Length;

        // to store the result
        List<int> res = new List<int>();
        for (int i = 0; i < n; i++) {
            res.Add(0);
        }

        // to store frequency of elements in b[]
        int[] cnt = new int[100001];
        for (int i = 0; i < 100001; i++) {
            cnt[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            cnt[b[i]]++;
        }

        // transform cnt[] to prefix sum array
        for (int i = 1; i < 100001; i++) {
            cnt[i] += cnt[i - 1];
        }

        // loop for each element of a[]
        for (int i = 0; i < n; i++) {
            res[i] = cnt[a[i]];
        }

        return res;
    }

    public static void Main(string[] args) {
        int[] a = {1, 2, 3, 4, 7, 9};
        int[] b = {0, 1, 2, 1, 1, 4};
        List<int> result = countEleLessThanOrEqual(a, b);
        foreach (int i in result) {
            Console.Write(i + " ");
        }
    }
}
JavaScript
// to store the result
function countEleLessThanOrEqual(a, b) {
    let n = a.length, m = b.length;

    // to store the result
    let res = new Array(n).fill(0);

    // to store frequency of elements in b[]
    let cnt = new Array(100001).fill(0);
    for (let i = 0; i < m; i++) {
        cnt[b[i]]++;
    }

    // transform cnt[] to prefix sum array
    for (let i = 1; i < 100001; i++) {
        cnt[i] += cnt[i - 1];
    }

    // loop for each element of a[]
    for (let i = 0; i < n; i++) {
        res[i] = cnt[a[i]];
    }

    return res;
}

let a = [1, 2, 3, 4, 7, 9];
let b = [0, 1, 2, 1, 1, 4];
let result = countEleLessThanOrEqual(a, b);
console.log(result.join(" "));

Output
4 5 5 6 6 6 


Next Article

Similar Reads