

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the overlapping sum of two arrays using C++
In this problem, we are given two arrays arr1[] and arr2[] consisting of unique values. Our task is to find the overlapping sum of two arrays.
All elements of the arrays are distinct. And we need to return the sum of elements which are common for both arrays
Let’s take an example to understand the problem,
Input
arr1[] = {5, 4, 9, 2}, arr2[] = {6, 3, 9, 4}
Output
2
Explanation
The elements that are present in both arrays are 9 and 4. The sum is 9 + 9 + 4 + 4 = 26
Solution Approach
A simple solution to the problem is traversing one array say arr1[] and for each element check if there is a matching value in another array. If any element matching the current value is found, then add both to the sum value.
This approach requires nesting of loops that leads to time complexity of the order O(N2).
Another approach to solve the problem is using hashing. We will create a hash table and store values of both the arrays in the table and keep the count of frequency of elements. Then add the value with occurrence frequency two. Return double of the sum value.
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h> using namespace std; int findCommonValSum(int A[], int B[], int n){ unordered_map<int,int> hashTable; for(int i=0;i<n;i++){ if(hashTable.find(A[i])==hashTable.end()) hashTable.insert(make_pair(A[i],1)); else hashTable[A[i]]++; if(hashTable.find(B[i])==hashTable.end()) hashTable.insert(make_pair(B[i],1)); else hashTable[B[i]]++; } int commSum = 0; for(auto itr = hashTable.begin(); itr!=hashTable.end(); itr++){ if((itr->second)==2){ commSum += (itr->first); } } return (commSum*2); } int main(){ int A[] = { 5, 4, 9, 2 }; int B[] = { 6, 3, 9, 4 }; int n = sizeof(A) / sizeof(A[0]); cout<<"The sum of common values in the array are "<<findCommonValSum(A, B, n); return 0; }
Output
The sum of common values in the array are 26
- Related Questions & Answers
- Program to find two non-overlapping sub-arrays each with target sum using Python
- Program to find maximum sum of two non-overlapping sublists in Python
- Maximum Sum of Two Non-Overlapping Subarrays in C++
- Find Sum of pair from two arrays with maximum sum in C++
- Maximum sum two non-overlapping subarrays of given size in C++
- Reverse sum of two arrays in JavaScript
- Maximum OR sum of sub-arrays of two different arrays in C++
- Program to find largest sum of 3 non-overlapping sublists of same sum in Python
- Maximum Sum of Products of Two Arrays in C++
- Program to find maximum number of non-overlapping subarrays with sum equals target using Python
- How to find the Sum of two Binary Numbers using C#?
- C++ program to find two numbers from two arrays whose sum is not present in both arrays
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Find sub-arrays from given two arrays such that they have equal sum in Python
- Find a pair of elements swapping which makes sum of two arrays same in C++