📌  相关文章
📜  第一个数组中元素的总和,使得第二个数组中小于或等于它们的元素数最大

📅  最后修改于: 2021-10-27 08:54:11             🧑  作者: Mango

给定两个未排序阵列ARR1 []ARR2 []中,任务是找到ARR1的元素的总和[],使得元件的小于或数量上等于它们ARR2 []为最大。

例子:

方法:上述问题的有效解决方案背后的想法是使用第二个数组的散列,然后找到散列数组的累积和。之后,可以轻松计算第二个数组中小于或等于第一个数组元素的元素数。这将给出一个频率数组,它表示第二个数组中小于或等于第一个数组元素的元素数,从中可以计算出与频率数组中的最大频率相对应的第一个数组元素的总和。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define MAX 100000
 
// Function to return the required sum
int findSumofEle(int arr1[], int m,
                 int arr2[], int n)
{
    // Creating hash array initially
    // filled with zero
    int hash[MAX] = { 0 };
 
    // Calculate the frequency
    // of elements of arr2[]
    for (int i = 0; i < n; i++)
        hash[arr2[i]]++;
 
    // Running sum of hash array
    // such that hash[i] will give count of
    // elements less than or equal to i in arr2[]
    for (int i = 1; i < MAX; i++)
        hash[i] = hash[i] + hash[i - 1];
 
    // To store the maximum value of
    // the number of elements in arr2[] which are
    // smaller than or equal to some element of arr1[]
    int maximumFreq = 0;
    for (int i = 0; i < m; i++)
        maximumFreq = max(maximumFreq, hash[arr1[i]]);
 
    // Calculate the sum of elements from arr1[]
    // corresponding to maximum frequency
    int sumOfElements = 0;
    for (int i = 0; i < m; i++)
        sumOfElements += (maximumFreq == hash[arr1[i]]) ? arr1[i] : 0;
 
    // Return the required sum
    return sumOfElements;
}
 
// Driver code
int main()
{
    int arr1[] = { 2, 5, 6, 8 };
    int arr2[] = { 4, 10 };
    int m = sizeof(arr1) / sizeof(arr1[0]);
    int n = sizeof(arr2) / sizeof(arr2[0]);
 
    cout << findSumofEle(arr1, m, arr2, n);
 
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
 
    static int MAX = 100000;
 
    // Function to return the required sum
    static int findSumofEle(int arr1[], int m,
                            int arr2[], int n)
    {
        // Creating hash array initially
        // filled with zero
        int hash[] = new int[MAX];
 
        // Calculate the frequency
        // of elements of arr2[]
        for (int i = 0; i < n; i++)
        {
            hash[arr2[i]]++;
        }
 
        // Running sum of hash array
        // such that hash[i] will give count of
        // elements less than or equal to i in arr2[]
        for (int i = 1; i < MAX; i++)
        {
            hash[i] = hash[i] + hash[i - 1];
        }
 
        // To store the maximum value of
        // the number of elements in arr2[] which are
        // smaller than or equal to some element of arr1[]
        int maximumFreq = 0;
        for (int i = 0; i < m; i++)
        {
            maximumFreq = Math.max(maximumFreq, hash[arr1[i]]);
        }
 
        // Calculate the sum of elements from arr1[]
        // corresponding to maximum frequency
        int sumOfElements = 0;
        for (int i = 0; i < m; i++)
        {
            sumOfElements += (maximumFreq == hash[arr1[i]]) ? arr1[i] : 0;
        }
 
        // Return the required sum
        return sumOfElements;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr1[] = {2, 5, 6, 8};
        int arr2[] = {4, 10};
        int m = arr1.length;
        int n = arr2.length;
 
        System.out.println(findSumofEle(arr1, m, arr2, n));
    }
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python 3 implementation of the approach
MAX = 100000
 
# Function to return the required sum
def findSumofEle(arr1, m, arr2, n):
     
    # Creating hash array initially
    # filled with zero
    hash = [0 for i in range(MAX)]
 
    # Calculate the frequency
    # of elements of arr2[]
    for i in range(n):
        hash[arr2[i]] += 1
 
    # Running sum of hash array
    # such that hash[i] will give count of
    # elements less than or equal to i in arr2[]
    for i in range(1, MAX, 1):
        hash[i] = hash[i] + hash[i - 1]
 
    # To store the maximum value of
    # the number of elements in arr2[]
    # which are smaller than or equal
    # to some element of arr1[]
    maximumFreq = 0
    for i in range(m):
        maximumFreq = max(maximumFreq,
                          hash[arr1[i]])
 
    # Calculate the sum of elements from arr1[]
    # corresponding to maximum frequency
    sumOfElements = 0
    for i in range(m):
        if (maximumFreq == hash[arr1[i]]):
            sumOfElements += arr1[i]
 
    # Return the required sum
    return sumOfElements
 
# Driver code
if __name__ == '__main__':
    arr1 = [2, 5, 6, 8]
    arr2 = [4, 10]
    m = len(arr1)
    n = len(arr2)
    print(findSumofEle(arr1, m, arr2, n))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    static int MAX = 100000;
 
    // Function to return the required sum
    static int findSumofEle(int[] arr1, int m,
                            int[] arr2, int n)
    {
        // Creating hash array initially
        // filled with zero
        int[] hash = new int[MAX];
 
        // Calculate the frequency
        // of elements of arr2[]
        for (int i = 0; i < n; i++)
        {
            hash[arr2[i]]++;
        }
 
        // Running sum of hash array
        // such that hash[i] will give count of
        // elements less than or equal to i in arr2[]
        for (int i = 1; i < MAX; i++)
        {
            hash[i] = hash[i] + hash[i - 1];
        }
 
        // To store the maximum value of
        // the number of elements in arr2[] which are
        // smaller than or equal to some element of arr1[]
        int maximumFreq = 0;
        for (int i = 0; i < m; i++)
        {
            maximumFreq = Math.Max(maximumFreq, hash[arr1[i]]);
        }
 
        // Calculate the sum of elements from arr1[]
        // corresponding to maximum frequency
        int sumOfElements = 0;
        for (int i = 0; i < m; i++)
        {
            sumOfElements += (maximumFreq == hash[arr1[i]]) ? arr1[i] : 0;
        }
 
        // Return the required sum
        return sumOfElements;
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr1 = {2, 5, 6, 8};
        int[] arr2 = {4, 10};
        int m = arr1.Length;
        int n = arr2.Length;
 
        Console.WriteLine(findSumofEle(arr1, m, arr2, n));
    }
}
 
// This code has been contributed by Code_Mech.


PHP


Javascript


输出:
19

时间复杂度: O(MAX)

辅助空间: O(MAX)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程