VOOZH about

URL: https://www.geeksforgeeks.org/dsa/element-1st-array-count-elements-less-equal-2nd-array/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 28 Jun, 2025

Given two unsorted arrays a[] and b[]. Both arrays may contain duplicate elements. For each element in a[], find the count of 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 iterates through each element in array a[], and for every element in a[], the inner loop traverses array b[] to count how many elements in b[] are less than or equal to the current element from a[].


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.


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.


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 core idea is to first pair each element of a[] with its original index and sort the array based on values. Similarly, b[] is sorted to enable efficient traversal. Using a two-pointer approach, we iterate through b[] just once, incrementing the pointer whenever the current element in b[] is less than or equal to the current element in a[]. This gives us the count of such elements efficiently. Finally, the count is stored at the original index of the corresponding element in a[], ensuring the result maintains the initial ordering of a[].


Output
4 5 5 6 6 6 

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

The main idea is to first construct a frequency array for b[] to record how often each number appears. This frequency array is then transformed into a prefix sum array, where each index holds the total count of elements in b[] that are less than or equal to that index. With this preprocessed data, the count for any element in a[] can then be retrieved in constant time.

Illustration:


Output
4 5 5 6 6 6 
Comment