Smallest Difference pair of values between two unsorted Arrays | function to calculate Small result between two arrays ; Sort both arrays using sort function ; Initialize result as max value ; Scan Both Arrays upto sizeof of the Arrays ; Move Smaller Value ; Return final sma result ; Input given array A ; Input given array B ; Calculate size of Both arrays ; Call function to print smallest result
function findSmallestDifference ( A , B , m , n ) { A . sort ( ( a , b ) => a - b ) ; B . sort ( ( a , b ) => a - b ) ; let a = 0 , b = 0 ; let result = Number . MAX_SAFE_INTEGER ; while ( a < m && b < n ) { if ( Math . abs ( A [ a ] - B [ b ] ) < result ) result = Math . abs ( A [ a ] - B [ b ] ) ; if ( A [ a ] < B [ b ] ) a ++ ; else b ++ ; } return result ; } let A = [ 1 , 2 , 11 , 5 ] ; let B = [ 4 , 12 , 19 , 23 , 127 , 235 ] ; let m = A . length ; let n = B . length ; document . write ( findSmallestDifference ( A , B , m , n ) ) ;