Minimum Possible value of | ai + aj | function for finding pairs and min value ; initialize smallest and count ; iterate over all pairs ; is Math . abs value is smaller than smallest update smallest and reset count to 1 ; if Math . abs value is equal to smallest increment count value ; print result ; driver program
function pairs ( arr , n , k ) { var smallest = 1000000000 ; var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) { if ( Math . abs ( arr [ i ] + arr [ j ] - k ) < smallest ) { smallest = Math . abs ( arr [ i ] + arr [ j ] - k ) ; count = 1 ; } else if ( Math . abs ( arr [ i ] + arr [ j ] - k ) == smallest ) count ++ ; } document . write ( " " + smallest + " " ) ; document . write ( " " + count + " " ) ; } var arr = [ 3 , 5 , 7 , 5 , 1 , 9 , 9 ] ; var k = 12 ; var n = arr . length ; pairs ( arr , n , k ) ;