2006. Count Number of Pairs With Absolute Difference K
On This Page
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
res = 0
cnt = Counter()
for num in nums:
res += cnt[num - k] + cnt[num + k]
cnt[num] += 1
return res