📜  找到两个数组的重叠和

📅  最后修改于: 2021-10-27 07:23:38             🧑  作者: Mango

给定两个数组 A[] 和 B[],每个数组都有 n 个唯一元素。任务是找到两个数组的重叠和。这是两个数组中共有的元素的总和。

注意:数组中的元素是唯一的。那就是数组不包含重复项。

例子:

Input : A[] = {1, 5, 3, 8}
        B[] = {5, 4, 6, 7}
Output : 10
Explanation : The element which is common in both arrays is 5.
Therefore, the overlapping sum will be (5+5) = 10

Input : A[] = {1, 5, 3, 8}
        B[] = {5, 1, 8, 3}
Output : 99

蛮力法:简单的方法是,对于 A[] 中的每个元素,检查它是否存在于 B[] 中,如果它存在于 B[] 中,则将该数字相加两次(一次是 A[],一次是 B []) 到总和。对数组 A[] 中的所有元素重复此过程。

时间复杂度:O(n^2)。

高效方法:一种有效的方法是使用哈希。遍历两个数组并将元素插入哈希表以跟踪元素的计数。将计数等于 2 的元素添加到 sum。

下面是上述方法的实现:

C++
// CPP program to find overlapping sum
#include 
using namespace std;
 
// Function for calculating
// overlapping sum of two array
int findSum(int A[], int B[], int n)
{  
    // unordered map to store count of
    // elements
    unordered_map hash;
     
    // insert elements of A[] into
    // unordered_map
    for(int i=0;isecond)==2)
        {
            sum += (itr->first)*2;
        }
    }
     
    return sum;
}
 
// driver code
int main()
{
    int A[] = { 5, 4, 9, 2, 3 };
    int B[] = { 2, 8, 7, 6, 3 };
 
    // size of array
    int n = sizeof(A) / sizeof(A[0]);
 
    // function call
    cout << findSum(A, B, n);
     
    return 0;
}


Java
// Java program to find overlapping sum
import java.io.*;
import java.util.*;
class GFG
{
   
    // Function for calculating
    // overlapping sum of two array
    static int findSum(int A[], int B[], int n)
    {
       
        // unordered map to store count of 
        // elements
        HashMap hash = new HashMap<>();
       
        // insert elements of A[] into
        // unordered_map
        for(int i = 0; i < n; i++)
        {
            if(!hash.containsKey(A[i]))
            {
                hash.put(A[i], 1);
            }
            else
            {
                hash.put(A[i], hash.get(A[i]) + 1);
            }
        }
       
        // insert elements of B[] into
        // unordered_map
        for(int i = 0; i < n; i++)
        {
            if(!hash.containsKey(B[i]))
            {
                hash.put(B[i], 1);
            }
            else
            {
                hash.put(B[i], hash.get(B[i]) + 1);
            }
        }
       
        // calculate overlapped sum
        int sum = 0;
        for(int itr: hash.keySet())
        {
            if(hash.get(itr) == 2)
            {
                sum += itr * 2;
            }
        }
        return sum;
    }
   
    // Driver code
    public static void main (String[] args)
    {
        int A[] = { 5, 4, 9, 2, 3 };
        int B[] = { 2, 8, 7, 6, 3 };
   
        // size of array
        int n = A.length;
        System.out.println(findSum(A, B, n));
    }
}
 
// This code is contributed by rag2127


Python3
# Python3 program to find overlapping sum
 
# Function for calculating
# overlapping sum of two array
def findSum(A, B, n):
     
    # unordered map to store count of
    # elements
    hash=dict()
 
    # insert elements of A into
    # unordered_map
    for i in range(n):
        hash[A[i]] = hash.get(A[i], 0) + 1
 
    # insert elements of B into
    # unordered_map
    for i in range(n):
        hash[B[i]] = hash.get(B[i], 0) + 1
 
 
    # calculate overlapped sum
    sum = 0
 
    for i in hash:
        if hash[i] == 2:
            sum += i * 2
 
    return sum
 
# Driver code
 
A = [ 5, 4, 9, 2, 3 ]
B = [ 2, 8, 7, 6, 3 ]
 
# size of array
n = len(A)
 
# function call
print(findSum(A, B, n))
 
# This code is contributed by mohit kumar 29


C#
// C# program to find overlapping sum
using System;
using System.Collections.Generic;
public class GFG
{
      
    // Function for calculating
    // overlapping sum of two array
    static int findSum(int[] A, int[] B, int n)
    {
       
        // unordered map to store count of 
        // elements
        Dictionary hash = new Dictionary();
         
        // insert elements of A[] into
        // unordered_map
        for(int i = 0; i < n; i++)
        {
            if(!hash.ContainsKey(A[i]))
            {
                hash.Add(A[i], 1);
            }
            else
            {
                hash[A[i]]++;
            }
        }
         
        // insert elements of B[] into
        // unordered_map
        for(int i = 0; i < n; i++)
        {
            if(!hash.ContainsKey(B[i]))
            {
                hash.Add(B[i], 1);
            }
            else
            {
                hash[B[i]]++;
            }
        }
         
        // calculate overlapped sum
        int sum = 0;
        foreach(KeyValuePair itr in hash)
        {
            if(itr.Value == 2)
            {
                sum += itr.Key * 2;
            }
        }
        return sum;
    }
     
    // Driver code
    static public void Main ()
    {
         int[] A = { 5, 4, 9, 2, 3 };
        int[] B = { 2, 8, 7, 6, 3 };
    
        // size of array
        int n = A.Length;
        Console.Write(findSum(A, B, n));
    }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出:

10

时间复杂度:O(n)
辅助空间:O(n)

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