📌  相关文章
📜  对于第一个数组中的每个元素,计数小于或等于第二个数组中的元素

📅  最后修改于: 2021-04-26 10:20:59             🧑  作者: Mango

给定两个未排序的数组arr1 []和arr2 []。它们可能包含重复项。对于arr1 []中的每个元素,计数小于或等于数组arr2 []中的元素。
资料来源: Amazon Interview Experience |套装354(对于SDE-2)
例子:

Input : arr1[] = [1, 2, 3, 4, 7, 9]
        arr2[] = [0, 1, 2, 1, 1, 4]
Output : [4, 5, 5, 6, 6, 6]
So the number of elements less than or equal to 
1 is 4, 2 is 5, 3 is 5, 4 is 6, 7 is 6 and 9 is 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]
So the number of elements less than or equal to 
5 is 4, 10 is 6, 2 is 1, 6 is 5, 1 is 0, 8 is 6, 
6 is 5 and 12 is 7 

天真的方法:
方法:想法是使用两个循环,外部循环用于数组arr1 []的元素,内部循环用于数组arr2 []的元素。然后,对于arr1 []的每个元素,在arr2 []中计数小于或等于它的元素。
算法 :

  1. 从头到尾遍历第一个数组的元素。
  2. 对于第一个数组中的每个元素。
  3. 遍历第二个数组中的元素并找到小于或等于第一个数组中元素的元素数。
  4. 打印每个索引的计数。
C++
// C++ code for the above algorithm
#include 
using namespace std;
 
// 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)
{
    // Run two loops to count
    // First loop to traverse the first array
    // Second loop to traverse the second array
    for (int i = 0; i < m; i++) {
        int count = 0;
 
        // Traverse through second array
        for (int j = 0; j < n; j++)
            if (arr2[j] <= arr1[i])
                count++;
 
        cout << count << " ";
    }
}
 
// Driver program to test above
int main()
{
    int arr1[] = { 1, 2, 3, 4, 7, 9 };
    int arr2[] = { 0, 1, 2, 1, 1, 4 };
    int m = sizeof(arr1) / sizeof(arr1[0]);
    int n = sizeof(arr2) / sizeof(arr2[0]);
    countEleLessThanOrEqual(arr1, arr2, m, n);
    return 0;
}


Java
import java.util.Arrays;
 
class GFG {
 
    // function to count for each
    // element in 1st array,
    // elements less than or equal
    // to it in 2nd array
    static int countEleLessThanOrEqual(
        int arr1[], int arr2[],
        int m, int n)
    {
        // Run two loops to count
        // First loop to traverse the first array
        // Second loop to traverse the second array
        for (int i = 0; i < m; i++) {
            int count = 0;
 
            // Traverse through second array
            for (int j = 0; j < n; j++)
                if (arr2[j] <= arr1[i])
                    count++;
            System.out.print(count + " ");
        }
        return m;
    }
 
    // Driver method
    public static void main(String[] args)
    {
 
        int arr1[] = { 1, 2, 3, 4, 7, 9 };
        int arr2[] = { 0, 1, 2, 1, 1, 4 };
        countEleLessThanOrEqual(
            arr1, arr2, arr1.length, arr2.length);
    }
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 code for the above algorithm
 
# function to count for each element in 1st array,
# elements less than or equal to it in 2nd array
def countEleLessThanOrEqual(arr1, arr2, m, n):
     
    # Run two loops to count
    # First loop to traverse the first array
    # Second loop to traverse the second array
    for i in range(m):
         
        count = 0
        # Traverse through second array
        for j in range(n):
            if (arr2[j] <= arr1[i]):
                count+= 1
             
        print(count, end =" ")
 
# Driver program to test above
arr1 = [1, 2, 3, 4, 7, 9]
arr2 = [0, 1, 2, 1, 1, 4]
m = len(arr1)
n = len(arr2)
countEleLessThanOrEqual(arr1, arr2, m, n)
  
# This code is contributed by shubhamsingh10


C#
// C# implementation of For each element
using System;
 
class GFG {
 
    // function 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)
    {
        // Run two loops to count
        // First loop to traverse the first array
        // Second loop to traverse the second array
        for (int i = 0; i < m; i++) {
            int count = 0;
 
            // Traverse through second array
            for (int j = 0; j < n; j++)
                if (arr2[j] <= arr1[i])
                    count++;
            Console.Write((count) + " ");
        }
    }
 
    // Driver method
    public static void Main()
    {
        int[] arr1 = { 1, 2, 3, 4, 7, 9 };
        int[] arr2 = { 0, 1, 2, 1, 1, 4 };
 
        countEleLessThanOrEqual(arr1,
                                arr2, arr1.Length, arr2.Length);
    }
}
 
// This code is contributed by mohit kumar 29.


Javascript


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 < m; i++) {
        // last index of largest element
        // smaller than or equal to x
        int index = binary_search(
            arr2, 0, n - 1, arr1[i]);
 
        // required count for the element arr1[i]
        cout << (index + 1) << " ";
    }
}
 
// Driver program to test above
int main()
{
    int arr1[] = { 1, 2, 3, 4, 7, 9 };
    int arr2[] = { 0, 1, 2, 1, 1, 4 };
    int m = sizeof(arr1) / sizeof(arr1[0]);
    int n = sizeof(arr2) / sizeof(arr2[0]);
    countEleLessThanOrEqual(arr1, arr2, m, n);
    return 0;
}


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 < m; i++) {
            // Last index of largest element
            // smaller than or equal to x
            int index = binary_search(
                arr2, 0, n - 1, arr1[i]);
 
            // Required count for the element arr1[i]
            System.out.print((index + 1) + " ");
        }
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int arr1[] = { 1, 2, 3, 4, 7, 9 };
        int arr2[] = { 0, 1, 2, 1, 1, 4 };
 
        countEleLessThanOrEqual(
            arr1, arr2, arr1.length,
            arr2.length);
    }
}


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 < m; i++) {
            // last index of largest
            // element smaller than or
            // equal to x
            int index = binary_search(
                arr2, 0, n - 1, arr1[i]);
 
            // required count for the
            // element arr1[i]
            Console.Write((index + 1) + " ");
        }
    }
 
    // Driver method
    public static void Main()
    {
        int[] arr1 = { 1, 2, 3, 4, 7, 9 };
        int[] arr2 = { 0, 1, 2, 1, 1, 4 };
 
        countEleLessThanOrEqual(arr1,
                                arr2, arr1.Length, arr2.Length);
    }
}
 
// This code is contributed by Sam007.


PHP


输出:

4 5 5 6 6 6 

复杂度分析:

  • 时间复杂度: O(m * n)。
    考虑到arr1 []arr2 []的大小分别为mn
  • 空间复杂度: O(1)。
    由于不需要额外的空间

高效的解决方案:
方法:对第二个数组的元素进行排序,即数组arr2 [] 。然后在数组arr2 []上执行修改后的二进制搜索。对于数组arr1 []的每个元素x ,在排序后的数组arr2 []中找到小于或等于x的最大元素的最后一个索引。最大元素的索引将给出元素的数量。
算法:

  1. 排序第二个数组。
  2. 从头到尾遍历第一个数组的元素。
  3. 对于第一个数组中的每个元素。
  4. 在第二个数组上执行二进制搜索,找到小于或等于第一个数组的元素的最大元素的索引。
  5. 最大元素的索引将给出元素的数量。打印每个索引的计数。

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 < m; i++) {
        // last index of largest element
        // smaller than or equal to x
        int index = binary_search(
            arr2, 0, n - 1, arr1[i]);
 
        // required count for the element arr1[i]
        cout << (index + 1) << " ";
    }
}
 
// Driver program to test above
int main()
{
    int arr1[] = { 1, 2, 3, 4, 7, 9 };
    int arr2[] = { 0, 1, 2, 1, 1, 4 };
    int m = sizeof(arr1) / sizeof(arr1[0]);
    int n = sizeof(arr2) / sizeof(arr2[0]);
    countEleLessThanOrEqual(arr1, arr2, m, n);
    return 0;
}

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 < m; i++) {
            // Last index of largest element
            // smaller than or equal to x
            int index = binary_search(
                arr2, 0, n - 1, arr1[i]);
 
            // Required count for the element arr1[i]
            System.out.print((index + 1) + " ");
        }
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int arr1[] = { 1, 2, 3, 4, 7, 9 };
        int arr2[] = { 0, 1, 2, 1, 1, 4 };
 
        countEleLessThanOrEqual(
            arr1, arr2, arr1.length,
            arr2.length);
    }
}

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 < m; i++) {
            // last index of largest
            // element smaller than or
            // equal to x
            int index = binary_search(
                arr2, 0, n - 1, arr1[i]);
 
            // required count for the
            // element arr1[i]
            Console.Write((index + 1) + " ");
        }
    }
 
    // Driver method
    public static void Main()
    {
        int[] arr1 = { 1, 2, 3, 4, 7, 9 };
        int[] arr2 = { 0, 1, 2, 1, 1, 4 };
 
        countEleLessThanOrEqual(arr1,
                                arr2, arr1.Length, arr2.Length);
    }
}
 
// This code is contributed by Sam007.

的PHP


输出:

4 5 5 6 6 6

复杂度分析:

  • 时间复杂度: O(mlogn + nlogn)。
    分别考虑大小为mn的arr1 []arr2 []
  • 空间复杂度: O(1)。
    由于不需要额外的空间