Pairs with difference k

Write codeMedium18 min

Your task

Given an integer array nums and an integer k (k >= 0), return the number of UNIQUE pairs of distinct array positions whose values differ by exactly k. A pair is identified by its value pair, so (1,3) counts once even if 1 or 3 repeats. For k = 0, count values that appear at least twice. Array length up to 1e4.

Examples

count_pairs_diff(nums: list[int], k: int) -> int
Example 1
Inputcount_pairs_diff([3, 1, 4, 1, 5], 2)
Output2