• + 0 comments
    def pairs(k, arr):
        # Convert the list to a set for O(1) lookups
        arr_set = set(arr)
        count = 0
        
        # Check for each element if there's a pair with difference k
        for x in arr:
            if x + k in arr_set:
                count += 1
            if x - k in arr_set:
                count += 1
        
        # Since each pair is counted twice, we return half the count
        return count // 2