2006. Count Number of Pairs With Absolute Difference K

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2006

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