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

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]

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]
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Naive Approach: Using two loops, outer loop for elements of array arr1[] and inner loop for elements of array arr2[]. Then for each element of arr1[], count elements less than or equal to it in arr2[].
Time complexity: O(m * n), considering arr1[] and arr2[] are of sizes m and n respectively.

Efficient Approach: Sort the elements of 2nd array, i.e., array arr2[]. Then perform a modified binary search on array arr2[]. For each element x of array arr1[], find the last index of the largest element smaller than or equal to x in sorted array arr2[].

## C++

 `// C++ implementation of For each element in 1st  ` `// array count elements less than or equal to it ` `// in 2nd array ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// function returns the index of largest element  ` `// smaller than equal to 'x' in 'arr'. For duplicates ` `// it returns the last index of occurrence of required ` `// element. If no such element exits then it returns -1.  ` `int` `binary_search(``int` `arr[], ``int` `l, ``int` `h, ``int` `x) ` `{ ` `    ``while` `(l <= h) ` `    ``{ ` `        ``int` `mid = (l+h) / 2; ` ` `  `        ``// if 'x' is greater than or equal to arr[mid],  ` `        ``// then search in arr[mid+1...h] ` `        ``if` `(arr[mid] <= x) ` `            ``l = mid + 1; ` ` `  `        ``// else search in arr[l...mid-1]     ` `        ``else` `            ``h = mid - 1;     ` `    ``} ` `     `  `    ``// required index ` `    ``return` `h; ` `} ` ` `  `// function to count for each element in 1st array, ` `// elements less than or equal to it in 2nd array ` `void` `countEleLessThanOrEqual(``int` `arr1[], ``int` `arr2[],  ` `                             ``int` `m, ``int` `n) ` `{ ` `    ``// sort the 2nd array ` `    ``sort(arr2, arr2+n); ` `     `  `    ``// for each element of 1st array ` `    ``for` `(``int` `i=0; i

## Java

 `// Java implementation of For each element in 1st  ` `// array count elements less than or equal to it ` `// in 2nd array ` ` `  `import` `java.util.Arrays; ` ` `  `class` `GFG ` `{ ` `    ``// method returns the index of largest element  ` `    ``// smaller than equal to 'x' in 'arr'. For duplicates ` `    ``// it returns the last index of occurrence of required ` `    ``// element. If no such element exits then it returns -1.  ` `    ``static` `int` `binary_search(``int` `arr[], ``int` `l, ``int` `h, ``int` `x) ` `    ``{ ` `        ``while` `(l <= h) ` `        ``{ ` `            ``int` `mid = (l+h) / ``2``; ` `      `  `            ``// if 'x' is greater than or equal to arr[mid],  ` `            ``// then search in arr[mid+1...h] ` `            ``if` `(arr[mid] <= x) ` `                ``l = mid + ``1``; ` `      `  `            ``// else search in arr[l...mid-1]     ` `            ``else` `                ``h = mid - ``1``;     ` `        ``} ` `          `  `        ``// required index ` `        ``return` `h; ` `    ``} ` `      `  `    ``// method to count for each element in 1st array, ` `    ``// elements less than or equal to it in 2nd array ` `    ``static` `void` `countEleLessThanOrEqual(``int` `arr1[], ``int` `arr2[],  ` `                                 ``int` `m, ``int` `n) ` `    ``{ ` `        ``// sort the 2nd array ` `        ``Arrays.sort(arr2); ` `          `  `        ``// for each element of 1st array ` `        ``for` `(``int` `i=``0``; i

## Python

 `# python implementation of For each element in 1st  ` `# array count elements less than or equal to it ` `# in 2nd array ` ` `  `# function returns the index of largest element  ` `# smaller than equal to 'x' in 'arr'. For duplicates ` `# it returns the last index of occurrence of required ` `# element. If no such element exits then it returns -1 ` `def` `bin_search(arr, n, x): ` `     `  `l ``=` `0` `h ``=` `n ``-` `1` `while``(l <``=` `h): ` `    ``mid ``=` `int``((l ``+` `h) ``/` `2``) ` `    ``# if 'x' is greater than or equal to arr[mid],  ` `    ``# then search in arr[mid + 1...h] ` `    ``if``(arr[mid] <``=` `x): ` `    ``l ``=` `mid ``+` `1``; ` `    ``else``: ` `    ``# else search in arr[l...mid-1] ` `    ``h ``=` `mid ``-` `1` `# required index ` `return` `h ` ` `  `# function to count for each element in 1st array, ` `# elements less than or equal to it in 2nd array ` `def` `countElements(arr1, arr2, m, n): ` `# sort the 2nd array ` `arr2.sort() ` ` `  `# for each element in first array ` `for` `i ``in` `range``(m): ` `    ``# last index of largest element  ` `    ``# smaller than or equal to x ` `    ``index ``=` `bin_search(arr2, n, arr1[i]) ` `    ``# required count for the element arr1[i] ` `    ``print``(index ``+` `1``) ` ` `  `# driver program to test above function ` `arr1 ``=` `[``1``, ``2``, ``3``, ``4``, ``7``, ``9``] ` `arr2 ``=` `[``0``, ``1``, ``2``, ``1``, ``1``, ``4``] ` `m ``=` `len``(arr1) ` `n ``=` `len``(arr2) ` `countElements(arr1, arr2, m, n) ` ` `  `#This code is contributed by Aditi Sharma `

## C#

 `// C# implementation of For each element ` `// in 1st array count elements less than ` `// or equal to it in 2nd array ` `using` `System; ` `         `  `public` `class` `GFG  ` `{ ` `     `  `    ``// method returns the index of ` `    ``// largest element smaller than ` `    ``// equal to 'x' in 'arr'. For  ` `    ``// duplicates it returns the last ` `    ``// index of occurrence of required ` `    ``// element. If no such element ` `    ``// exits then it returns -1.  ` `    ``static` `int` `binary_search(``int` `[]arr, ` `                     ``int` `l, ``int` `h, ``int` `x) ` `    ``{ ` `        ``while` `(l <= h) ` `        ``{ ` `            ``int` `mid = (l+h) / 2; ` `     `  `            ``// if 'x' is greater than or ` `            ``// equal to arr[mid], then  ` `            ``// search in arr[mid+1...h] ` `            ``if` `(arr[mid] <= x) ` `                ``l = mid + 1; ` `     `  `            ``// else search in ` `            ``// arr[l...mid-1]  ` `            ``else` `                ``h = mid - 1;  ` `        ``} ` `         `  `        ``// required index ` `        ``return` `h; ` `    ``} ` `     `  `    ``// method to count for each element ` `    ``// in 1st array, elements less than ` `    ``// or equal to it in 2nd array ` `    ``static` `void` `countEleLessThanOrEqual( ` `                 ``int` `[]arr1, ``int` `[]arr2, ` `                           ``int` `m, ``int` `n) ` `    ``{ ` `         `  `        ``// sort the 2nd array ` `        ``Array.Sort(arr2); ` `         `  `        ``// for each element of 1st array ` `        ``for` `(``int` `i=0; i

## PHP

 ` `

Output:

```4 5 5 6 6 6
```

Time Complexity: O(mlogn + nlogn), considering arr1[] and arr2[] are of sizes m and n respectively.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.