Pairs With Difference K
Count the pairs of positions (i, j) of the list nums with i < j such that the absolute difference abs(nums[i] - nums[j]) equals the number k.
Example 1
nums [1,2,2,1]
k 1
returns 4
Each 1 pairs with each 2: 4 pairs.
Example 2
nums [3,2,1,5,4]
k 2
returns 3
Pairs (3,1), (3,5), (2,4): 3.
Constraints
1 <= len(nums) <= 40-20 <= nums[i] <= 200 <= k <= 40
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.