Sudo Placement | Beautiful Pairs | The function to check if beautiful pair exists ; Set for hashing ; Traversing the first array ; Traversing the second array to check for every j corresponding to single i ; x + y = z = > x = y - z ; if such x exists then we return true ; hash to make use of it next time ; no pair exists ; Driver Code ; If pair exists then 1 else 0 2 nd argument as size of first array fourth argument as sizeof 2 nd array
function pairExists ( arr1 , m , arr2 , n ) { let s = new Set ( ) ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( s . has ( arr2 [ j ] - arr1 [ i ] ) ) return true ; } s . add ( arr1 [ i ] ) ; } return false ; } let arr1 = [ 1 , 5 , 10 , 8 ] ; let arr2 = [ 2 , 20 , 13 ] ; if ( pairExists ( arr1 , 4 , arr2 , 3 ) ) document . write ( 1 + " " ) ; else document . write ( 0 + " " ) ;