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
using System ; using System . Collections . Generic ; class GFG { static String checkPossible ( int [ ] A , int [ ] B , int n ) { HashSet values = new HashSet ( ) ; for ( int i = 0 ; i < n ; i ++ ) { values . Add ( B [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( values . Contains ( A [ i ] + A [ j ] ) ) { values . Remove ( A [ i ] + A [ j ] ) ; if ( values . Count == 0 ) break ; } } } if ( values . Count == 0 ) return " Yes " ; else return " No " ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] A = { 3 , 5 , 1 , 4 , 2 } ; int [ ] B = { 3 , 4 , 5 , 6 , 7 } ; Console . Write ( checkPossible ( A , B , N ) ) ; } }