Non | CPP program to find Non - overlapping sum ; function for calculating Non - overlapping sum of two array ; Insert elements of both arrays ; calculate non - overlapped sum ; driver code ; size of array ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int A [ ] , int B [ ] , int n ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) { hash [ A [ i ] ] ++ ; hash [ B [ i ] ] ++ ; } int sum = 0 ; for ( auto x : hash ) if ( x . second == 1 ) sum += x . first ; 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 ; }