Count Number of Pairs With Absolute Difference K
You have an integer array nums
and another integer k
. Your task is to find the number of pairs (i, j)
where i < j
, and the absolute difference between nums[i]
and nums[j]
is equal to k
.
Solution Approach
Here’s a stepbystep guide to solve this problem:
Initialize a Counter: Start with a variable to keep track of the count of pairs with the absolute difference
k
.Iterate Through the Array: Use two nested loops to iterate through all pairs of
(i, j)
wherei < j
.Calculate Absolute Difference: For each pair, calculate the absolute difference between
nums[i]
andnums[j]
.Check the Difference: If the absolute difference is equal to
k
, increment the counter.Return the Result: Return the count after iterating through all pairs.


Key Takeaways
 We are looking for pairs
(i, j)
where the absolute difference between the corresponding elements isk
.  We need to iterate through all possible pairs and calculate the absolute difference to find the count of such pairs.
 Using two nested loops and a counter variable allows us to solve this problem in a straightforward manner.