For each element in 1st array count elements less than or equal to it in 2nd array | Set 2

Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[].

Examples:

Input : arr1[] = [1, 2, 3, 4, 7, 9]
arr2[] = [0, 1, 2, 1, 1, 4]
Output : [4, 5, 5, 6, 6, 6]
Explanation: There are 4 elements less than or equal to 1 in second array, similarly there are 5 elements less than 2 in second array, calculate the values similarly for other elements.

Input : arr1[] = [5, 10, 2, 6, 1, 8, 6, 12]
arr2[] = [6, 5, 11, 4, 2, 3, 7]
Output : [4, 6, 1, 5, 0, 6, 5, 7]
Explanation: There are 4 elements less than or equal to 5 in second array, similarly there are 6 elements less than 10 in second array, calculate the values similarly for other elements.

This problem is already discussed in the previous post.



Solution: In this article, a more optimized linear time solution to the above problem is discussed. The approach discussed here works for arrays with values in a small range. A range of values that can be used as an index in an array.

Article Tags :
Practice Tags :