Pairs with Difference less than K | Function to count pairs ; Driver code
function countPairs ( a , n , k ) { var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( a [ j ] - a [ i ] ) < k ) res ++ ; return res ; } var a = [ 1 , 10 , 4 , 2 ] ; var k = 3 ; var n = a . length ; document . write ( countPairs ( a , n , k ) ) ;