Count of all pairs in an Array with minimum absolute difference | Function to return the count of all pairs having minimal absolute difference ; Stores the count of pairs ; Sort the array ; Stores the minimum difference between adjacent pairs ; Update the minimum difference between pairs ; Increase count of pairs with difference equal to that of minimum difference ; Return the final count ; Given array arr [ ] ; Function Call
function numberofpairs ( arr , N ) { let answer = 0 ; arr . sort ( ) ; let minDiff = Number . MAX_VALUE ; for ( let i = 0 ; i < N - 1 ; i ++ ) minDiff = Math . min ( minDiff , arr [ i + 1 ] - arr [ i ] ) ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i + 1 ] - arr [ i ] == minDiff ) answer ++ ; } return answer ; } let arr = [ 4 , 2 , 1 , 3 ] ; let N = arr . length ; document . write ( numberofpairs ( arr , N ) ) ;