Trains the technique from
LeetCode 532K-diff Pairs in an ArrayThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A probe wrote down n temperature readings into nums, in the order it took them. Readings can be negative and the same reading can be written down more than once. You are also given the gap k, which is not negative.
Two different positions i and j form a match when the absolute difference between nums[i] and nums[j] is exactly k. Matches are identified by the pair of values they use and nothing else: two matches that carry the same two values are the same match, and swapping which value comes first does not create a new one.
Return the number of distinct matches. Note that when k is 0 a match uses one value twice, so it needs that value to have been written down at two different positions.
Example 1
The value pairs sitting three apart are (1, 4) and (4, 7), so there are two distinct matches.
Example 2
Only the value pair (2, 6) sits four apart. It shows up at several combinations of positions, but they all carry the same two values and count once.
Example 3
With a gap of zero, the values 4 and 6 each appear at two positions and give one match apiece, while 5 and 7 appear once and give none.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def find_pairs(nums: list[int], k: int) -> int:public int findPairs(int[] nums, int k)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.