Skip to the code
cf.
+25 XP

#32 Distinct Price Pairs

medium

A bundle builder suggests pairs of prices that add up to target. It should list each pair of values once, even if the same prices appear many times in nums.

Write uniquePairs(nums, target) that returns every distinct pair [a, b] with a <= b and a + b === target, where a and b come from two different indexes. Sort the pairs by a, smallest first.

Example 1

Input: nums = [1,5,3,3,7,5,-1], target = 6
Output: [[-1,7],[1,5],[3,3]]

Each pair of values is listed once. [3, 3] works because 3 appears twice.

Example 2

Input: nums = [3,4], target = 6
Output: []

3 + 3 would need two different 3s.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i], target <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).