Count distinct elements after adding each element of First Array with Second Array | C ++ program for the above approach ; Function to find Occurrence of each element from 1 to 2 * MAX ; Initialise MAX ; Count vector to store count of each element from 1 to 2 * MAX ; Size of Arr1 and Arr2 ; Find the elements of arr3 [ ] and increase count of element by 1 ; Print the result ; Driver Code ; Given arrays arr1 [ ] and arr2 [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findCount ( vector < int > & Arr1 , vector < int > & Arr2 ) { int MAX = max ( * max_element ( Arr1 . begin ( ) , Arr1 . end ( ) ) , * max_element ( Arr2 . begin ( ) , Arr2 . end ( ) ) ) ; vector < int > Count ( 2 * MAX + 1 , 0 ) ; int n = Arr1 . size ( ) , m = Arr2 . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int element = Arr1 [ i ] + Arr2 [ j ] ; Count [ element ] ++ ; } } for ( int i = 1 ; i <= 2 * MAX ; i ++ ) { if ( Count [ i ] > 0 ) { cout << i << " - > " << Count [ i ] << endl ; } } } int main ( ) { vector < int > arr1 = { 1 , 2 } ; vector < int > arr2 = { 1 , 2 , 1 } ; findCount ( arr1 , arr2 ) ; }