Pair with exact difference

Write codeMedium14 min

Your task

Given a sorted array of distinct integers nums and a positive integer d, return True if some pair of elements differs by exactly d, and False otherwise. Use two pointers that both move forward (a fast and a slow index) for an O(n) scan, no nested loops, no hash set. Example: nums = [1, 3, 6, 10], d = 4 gives True because 10 - 6 = 4.

Examples

has_pair_with_gap(nums: list[int], d: int) -> bool
Example 1
Inputhas_pair_with_gap([1, 3, 6, 10], 4)
OutputTrue