Find the first element arr [ j ] such that ( arr [ j ] - arr [ i ] ) >= K This is because after this element , all the elements will have absolute difference with arr [ i ] >= k and the count of valid pairs will be ( n - j )
while ( j < n && ( arr [ j ] - arr [ i ] ) < k ) j ++ ;