Unique Near-Equal Target-Sum Pairs
Learn this problemProblem statement
Given an integer array nums and an integer target, return every unique pair of values whose sum is target and whose absolute difference is at most 1.
Each pair must use two distinct array positions. Return each pair as [a, b], where a <= b. Pairs are unique by value, so repeated occurrences do not create duplicate result pairs. An equal-value pair [a, a] is valid only when a occurs at least twice.
Sort the returned pairs lexicographically. If no pair qualifies, return an empty array.
Function
findUniquePairs(nums: int[], target: int) → int[][]Examples
Example 1
nums = [1,4,2,3,2]target = 5return = [[2,3]]Both [1,4] and [2,3] sum to 5, but only [2,3] has an absolute difference at most 1. The repeated 2 does not duplicate the value pair.
Example 2
nums = [2,2,1,3]target = 4return = [[2,2]]The two different positions containing 2 form [2,2]. Although [1,3] also sums to 4, its absolute difference is 2.
Example 3
nums = [-3,-1,-2,0]target = -3return = [[-2,-1]]The pair [-2,-1] sums to -3 and its absolute difference is 1.
Example 4
nums = [1,5,9]target = 10return = []The values 1 and 9 sum to 10, but their absolute difference is greater than 1. The value 5 occurs only once, so [5,5] cannot be formed.
Constraints
0 <= nums.length <= 2 * 10^5-10^9 <= nums[i] <= 10^9-2 * 10^9 <= target <= 2 * 10^9