Count Number of Pairs With Absolute Difference K LeetCode solution in Python
1class Solution:2def countKDifference(self, nums: List[int], k: int) -> int:3seen = defaultdict(int)4counter = 05for num in nums:6tmp, tmp2 = num - k, num + k7if tmp in seen:8counter += seen[tmp]9if tmp2 in seen:10counter += seen[tmp2]1112seen[num] += 11314return counter