Let the set A be the set of integers of interest. Subtract K from each components and form set B. Sort the combined set of A and B. Linear traverse the sorted set and increment counter by 1 if neighboring items are the same. The counter value is the ans required.