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]

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]

This problem is already discussed in the previous post. 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 small range. A range of values that can be used as index in an array.



The idea is to use an array to create a direct address table initially filled with zero, such that hash[i] gives the count of an element in the second array arr2[]. Now, calculate pre-sum of the hash array. Doing this, hash[i] will now give the count of elements less than or equal to in second array arr2[].

Now, traverse the first array and print hash[arr1[i]].

Below program illustrate the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for each element in 1st 
// array count elements less than or equal to it 
// in 2nd array 
  
#include <iostream> 
using namespace std; 
  
#define MAX 100000 
  
// Function for each element in 1st 
// array count elements less than or equal to it 
// in 2nd array 
void countEleLessThanOrEqual(int arr1[], int m, 
                                int arr2[], int n) 
    // Creating hash array initially 
    // filled with zero 
    int hash[MAX] = {0}; 
      
    // Insert element of arr2[] to hash 
    // such that hash[i] will give count of 
    // element i in arr2[] 
    for (int i = 0; i < n; i++) 
        hash[arr2[i]]++; 
  
    // Presum of hash array 
    // such that hash[i] will give count of 
    // element less than or equals to i in arr2[] 
    for (int i=1; i<MAX; i++)     
        hash[i] = hash[i] + hash[i-1]; 
      
    // Traverse arr1[] and print hash[arr[i]] 
    for (int i = 0; i < m; i++) 
        cout << hash[arr1[i]] << " ";     
  
// Driver code 
int main() 
    int arr1[] = {1, 2, 3, 4, 7, 9}; 
    int arr2[] = {0, 1, 2, 1, 1, 4}; 
    int m, n; 
  
    m = sizeof(arr1) / sizeof(arr1[0]); 
    n = sizeof(arr2) / sizeof(arr2[0]); 
      
    countEleLessThanOrEqual(arr1, m, arr2, n); 
  
    return 0; 
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for each element 
// in 1st array count elements 
// less than or equal to it in 
// 2nd array 
import java.io.*;
  
class GFG 
{
static int MAX = 100000;
  
// Function for each element 
// in 1st array count elements 
// less than or equal to it 
// in 2nd array 
static void countEleLessThanOrEqual(int arr1[], int m, 
                                    int arr2[], int n)
    // Creating hash array initially
    // filled with zero
    int hash[] = new int[MAX];
      
    // Insert element of arr2[] to
    // hash such that hash[i] will
    // give count of element i in arr2[]
    for (int i = 0; i < n; i++)
        hash[arr2[i]]++;
  
    // Presum of hash array
    // such that hash[i] will 
    // give count of element 
    // less than or equals to 
    // i in arr2[]
    for(int i = 1; i < MAX; i++)
    {
        hash[i] = hash[i] + 
                  hash[i - 1];
    }
      
    // Traverse arr1[] and 
    // print hash[arr[i]]
    for (int i = 0; i < m; i++) 
    {
        System.out.print(hash[arr1[i]] + " ");
    }
}
  
// Driver code
public static void main (String[] args) 
{
    int arr1[] = {1, 2, 3, 4, 7, 9};
    int arr2[] = {0, 1, 2, 1, 1, 4};
    int m, n;
      
    m = arr1.length;
    n = arr2.length;
      
    countEleLessThanOrEqual(arr1, m, arr2, n);
}
}
  
// This code is contributed
// by inder_verma
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program for each element in 1st 
# array count elements less than or equal 
# to it in 2nd array 
  
MAX = 100000
  
# Function for each element in 1st 
# array count elements less than or 
# equal to it in 2nd array 
def countEleLessThanOrEqual(arr1, m, arr2, n):
      
    # Creating hash array initially 
    # filled with zero 
    hash = [0 for i in range(MAX)] 
      
    # Insert element of arr2[] to hash 
    # such that hash[i] will give count 
    # of element i in arr2[] 
    for i in range(n):
        hash[arr2[i]] += 1
  
    # Presum of hash array such that 
    # hash[i] will give count of element 
    # less than or equals to i in arr2[] 
    for i in range(1, MAX, 1):
        hash[i] = hash[i] + hash[i - 1
      
    # Traverse arr1[] and print hash[arr[i]] 
    for i in range(m):
        print(hash[arr1[i]], end = " ")     
  
# Driver code 
if __name__ == '__main__':
    arr1 = [1, 2, 3, 4, 7, 9
    arr2 = [0, 1, 2, 1, 1, 4
    m = len(arr1) 
    n = len(arr2)
      
    countEleLessThanOrEqual(arr1, m, arr2, n) 
      
# This code is contributed by
# Shashank_Sharma
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for each element 
// in 1st array count elements 
// less than or equal to it in 
// 2nd array 
using System;
public class GFG { 
  
static int MAX = 100000; 
  
// Function for each element 
// in 1st array count elements 
// less than or equal to it 
// in 2nd array 
static void countEleLessThanOrEqual(int []arr1, int m, 
                                    int []arr2, int n) 
    // Creating hash array initially 
    // filled with zero 
    int []hash = new int[MAX]; 
      
    // Insert element of arr2[] to 
    // hash such that hash[i] will 
    // give count of element i in arr2[] 
    for (int i = 0; i < n; i++) 
        hash[arr2[i]]++; 
  
    // Presum of hash array 
    // such that hash[i] will 
    // give count of element 
    // less than or equals to 
    // i in arr2[] 
    for(int i = 1; i < MAX; i++) 
    
        hash[i] = hash[i] + 
                hash[i - 1]; 
    
      
    // Traverse arr1[] and 
    // print hash[arr[i]] 
    for (int i = 0; i < m; i++) 
    
        Console.Write(hash[arr1[i]] + " "); 
    
  
// Driver code 
public static void Main () 
    int []arr1 = {1, 2, 3, 4, 7, 9}; 
    int []arr2 = {0, 1, 2, 1, 1, 4}; 
    int m, n; 
      
    m = arr1.Length; 
    n = arr2.Length; 
      
    countEleLessThanOrEqual(arr1, m, arr2, n); 
  
  
      
// This code is contributed by Shikha Singh. 
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program for each element in 1st 
// array count elements less than or 
// equal to it in 2nd array 
$MAX = 100000 ;
  
// Function for each element in 1st 
// array count elements less than or 
// equal to it in 2nd array 
function countEleLessThanOrEqual(&$arr1, $m
                                 &$arr2, $n
    global $MAX;
      
    // Creating hash array initially 
    // filled with zero 
    $hash = array_fill(0, $MAX, NULL); 
      
    // Insert element of arr2[] to hash 
    // such that hash[i] will give count of 
    // element i in arr2[] 
    for ($i = 0; $i < $n; $i++) 
        $hash[$arr2[$i]]++; 
  
    // Presum of hash array such that hash[i] 
    // will give count of element less than 
    // or equals to i in arr2[] 
    for ($i = 1; $i < $MAX; $i++)     
        $hash[$i] = $hash[$i] + $hash[$i - 1]; 
      
    // Traverse arr1[] and print hash[arr[i]] 
    for ($i = 0; $i < $m; $i++) 
        echo $hash[$arr1[$i]] . " ";     
  
// Driver code 
$arr1 = array(1, 2, 3, 4, 7, 9);
$arr2 = array(0, 1, 2, 1, 1, 4); 
  
$m = sizeof($arr1); 
$n = sizeof($arr2); 
  
countEleLessThanOrEqual($arr1, $m, $arr2, $n); 
  
// This code is contributed 
// by ChitraNayal
?>
chevron_right

Output:
4 5 5 6 6 6

Another method:(Using vector and map)

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for each element in 1st 
// array count elements less than or equal to it 
// in 2nd array 
#include <iostream>
#include <map>
#include <vector>
  
// Function for each element in 1st 
// array count elements less than or equal to it 
// in 2nd array 
void countLessThanOrEqual(const std::vector<int>& vec1, 
                        const std::vector<int>& vec2) {
    std::map<int, unsigned int> countOfVec2;
    for (const auto& item : vec2) {
        ++countOfVec2[item];
    }
  
    unsigned int prev = 0;
    for (auto& pair : countOfVec2) {
        pair.second += prev;
        prev = pair.second;
    }
    // Traverse arr1[] and print result 
    for (const auto& item : vec1) {
        unsigned int result = (--countOfVec2.upper_bound(item))->second;
        std::cout << result << " ";
    }
}
  
// Driver code
int main()
{
    std::vector<int> arr1 = { 1, 2, 3, 4, 7, 9 };
    std::vector<int> arr2 = { 0, 1, 2, 1, 1, 4 };
  
    countLessThanOrEqual(arr1, arr2);
  
    return 0;
}
chevron_right

Output:
4 5 5 6 6 6



Check out this Author's contributed articles.

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :