Check if each element of an Array is the Sum of any two elements of another Array | C ++ program to implement the above approach ; Function to check if each element of B [ ] can be formed by adding two elements of array A [ ] ; Store each element of B [ ] ; Traverse all possible pairs of array ; If A [ i ] + A [ j ] is present in the set ; Remove A [ i ] + A [ j ] from the set ; If set is empty ; Otherwise ; Driver Code
#include using namespace std ; string checkPossible ( int A [ ] , int B [ ] , int n ) { unordered_set values ; for ( int i = 0 ; i < n ; i ++ ) { values . insert ( B [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( values . find ( A [ i ] + A [ j ] ) != values . end ( ) ) { values . erase ( A [ i ] + A [ j ] ) ; if ( values . empty ( ) ) break ; } } } if ( values . size ( ) == 0 ) return " Yes " ; else return " No " ; } int main ( ) { int N = 5 ; int A [ ] = { 3 , 5 , 1 , 4 , 2 } ; int B [ ] = { 3 , 4 , 5 , 6 , 7 } ; cout << checkPossible ( A , B , N ) ; }