Check if each element of an Array is the Sum of any two elements of another Array | 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
function checkPossible ( A , B , n ) { var values = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) { values . add ( B [ i ] ) ; } for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < n ; j ++ ) { if ( values . has ( A [ i ] + A [ j ] ) ) { values . delete ( A [ i ] + A [ j ] ) ; if ( values . size == 0 ) break ; } } } if ( values . size == 0 ) return " " ; else return " " ; } var N = 5 ; var A = [ 3 , 5 , 1 , 4 , 2 ] ; var B = [ 3 , 4 , 5 , 6 , 7 ] ; document . write ( checkPossible ( A , B , N ) ) ;