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
def checkPossible ( A , B , n ) : NEW_LINE INDENT values = set ( [ ] ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT values . add ( B [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( ( A [ i ] + A [ j ] ) in values ) : NEW_LINE INDENT values . remove ( A [ i ] + A [ j ] ) NEW_LINE if ( len ( values ) == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( len ( values ) == 0 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE A = [ 3 , 5 , 1 , 4 , 2 ] NEW_LINE B = [ 3 , 4 , 5 , 6 , 7 ] NEW_LINE print ( checkPossible ( A , B , N ) ) NEW_LINE DEDENT