Skip to content
RED SAGSkills

Pairs With an Exact Price Gap

A price tracker stores a list of prices nums. Two different positions i < j form a *gap pair* when the absolute difference of their prices is exactly k.

Return the number of gap pairs.

Example 1

Input: nums = [1, 5, 3, 4, 2], k = 2
Output: 3
Explanation: The pairs are (1, 3), (5, 3) and (4, 2).

Example 2

Input: nums = [1, 1, 1, 1], k = 0
Output: 6
Explanation: Every pair of the four equal prices has gap 0.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 0 <= k <= 10^9
Hint 1

For each price, the partners it can pair with have value x - k or x + k.

Hint 2

Keep a count of every value seen so far, and be careful not to count k = 0 twice.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Automation Test Engineer, Data Engineer

Run checks your code on 2 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.