Find the overlapping sum of two arrays
Last Updated :
19 Sep, 2023
Given two arrays A[] and B[] having n unique elements each. The task is to find the overlapping sum of the two arrays. That is the sum of elements that is common in both of the arrays.
Note: Elements in the arrays are unique. That is the array does not contain duplicates.
Examples:
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 : 34
Brute Force Method: The simple approach is that for each element in A[] check whether it is present in B[] and if it is present in B[] then add that number two times(once for A[] and once for B[]) to the sum. Repeat this procedure for all elements in array A[].
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int A[], int B[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if (A[i] == B[j]) {
sum += 2 * A[i];
break ;
}
}
}
return sum;
}
int main()
{
int A[] = { 5, 4, 9, 2, 3 };
int B[] = { 2, 8, 7, 6, 3 };
int n = sizeof (A) / sizeof (A[0]);
cout << findSum(A, B, n);
return 0;
}
|
Java
import java.io.*;
class Gfg {
public static int findSum( int [] A, int [] B, int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
if (A[i] == B[j]) {
sum += 2 * A[i];
break ;
}
}
}
return sum;
}
public static void main(String[] args)
{
int [] A = { 5 , 4 , 9 , 2 , 3 };
int [] B = { 2 , 8 , 7 , 6 , 3 };
int n = A.length;
System.out.println(findSum(A, B, n));
}
}
|
Python3
def findSum( A, B, n):
sum = 0 ;
for i in range ( 0 ,n):
for j in range ( 0 ,n):
if (A[i] = = B[j]):
sum + = 2 * A[i];
break ;
return sum ;
A = [ 5 , 4 , 9 , 2 , 3 ];
B = [ 2 , 8 , 7 , 6 , 3 ];
n = len (A);
print (findSum(A, B, n));
|
C#
using System;
public class Gfg {
public static int findSum( int [] A, int [] B, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if (A[i] == B[j]) {
sum += 2 * A[i];
break ;
}
}
}
return sum;
}
public static void Main( string [] args)
{
int [] A = { 5, 4, 9, 2, 3 };
int [] B = { 2, 8, 7, 6, 3 };
int n = A.Length;
Console.WriteLine(findSum(A, B, n));
}
}
|
Javascript
function findSum( A, B, n)
{
let sum = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (A[i] == B[j]) {
sum += 2 * A[i];
break ;
}
}
}
return sum;
}
let A = [ 5, 4, 9, 2, 3 ];
let B = [ 2, 8, 7, 6, 3 ];
let n = A.length;
console.log(findSum(A, B, n));
|
Time Complexity: O(n^2).
Auxiliary Space: O(1)
Efficient Method: An efficient method is to use Hashing. Traverse both of the arrays and insert the elements into a hash table to keep track of the count of elements. Add the elements to sum whose count equals to two.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int A[], int B[], int n)
{
unordered_map< int , int > hash;
for ( int i=0;i<n;i++)
{
if (hash.find(A[i])==hash.end())
{
hash.insert(make_pair(A[i],1));
}
else
{
hash[A[i]]++;
}
}
for ( int i=0;i<n;i++)
{
if (hash.find(B[i])==hash.end())
{
hash.insert(make_pair(B[i],1));
}
else
{
hash[B[i]]++;
}
}
int sum = 0;
for ( auto itr = hash.begin(); itr!=hash.end(); itr++)
{
if ((itr->second)==2)
{
sum += (itr->first)*2;
}
}
return sum;
}
int main()
{
int A[] = { 5, 4, 9, 2, 3 };
int B[] = { 2, 8, 7, 6, 3 };
int n = sizeof (A) / sizeof (A[0]);
cout << findSum(A, B, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int findSum( int A[], int B[], int n)
{
HashMap<Integer, Integer> hash = new HashMap<>();
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 );
}
}
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 );
}
}
int sum = 0 ;
for ( int itr: hash.keySet())
{
if (hash.get(itr) == 2 )
{
sum += itr * 2 ;
}
}
return sum;
}
public static void main (String[] args)
{
int A[] = { 5 , 4 , 9 , 2 , 3 };
int B[] = { 2 , 8 , 7 , 6 , 3 };
int n = A.length;
System.out.println(findSum(A, B, n));
}
}
|
Python3
def findSum(A, B, n):
hash = dict ()
for i in range (n):
hash [A[i]] = hash .get(A[i], 0 ) + 1
for i in range (n):
hash [B[i]] = hash .get(B[i], 0 ) + 1
sum = 0
for i in hash :
if hash [i] = = 2 :
sum + = i * 2
return sum
A = [ 5 , 4 , 9 , 2 , 3 ]
B = [ 2 , 8 , 7 , 6 , 3 ]
n = len (A)
print (findSum(A, B, n))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int findSum( int [] A, int [] B, int n)
{
Dictionary< int , int > hash = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (!hash.ContainsKey(A[i]))
{
hash.Add(A[i], 1);
}
else
{
hash[A[i]]++;
}
}
for ( int i = 0; i < n; i++)
{
if (!hash.ContainsKey(B[i]))
{
hash.Add(B[i], 1);
}
else
{
hash[B[i]]++;
}
}
int sum = 0;
foreach (KeyValuePair< int , int > itr in hash)
{
if (itr.Value == 2)
{
sum += itr.Key * 2;
}
}
return sum;
}
static public void Main ()
{
int [] A = { 5, 4, 9, 2, 3 };
int [] B = { 2, 8, 7, 6, 3 };
int n = A.Length;
Console.Write(findSum(A, B, n));
}
}
|
Javascript
<script>
function findSum(A, B, n)
{
let hash = new Map();
for (let i = 0; i < n; i++)
{
if (!hash.has(A[i]))
{
hash.set(A[i], 1);
}
else
{
hash.set(A[i], hash.get(A[i]) + 1);
}
}
for (let i = 0; i < n; i++)
{
if (!hash.has(B[i]))
{
hash.set(B[i], 1);
}
else
{
hash.set(B[i], hash.get(B[i]) + 1);
}
}
let sum = 0;
for (let [key, value] of hash.entries())
{
if (value == 2)
{
sum += key * 2;
}
}
return sum;
}
let A = [ 5, 4, 9, 2, 3 ];
let B = [ 2, 8, 7, 6, 3 ];
let n = A.length;
document.write(findSum(A, B, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...