Pair Sum

Easy
ArrayHash TableTwo PointersSortingBYOPPrivate

Problem Statement

Given an array of integers nums and an integer target, find all unique pairs of numbers in nums that add up to the target. Return the pairs sorted lexicographically.

Input Format

The input consists of two lines:

  1. The first line contains an array of integers nums separated by spaces.
  2. The second line contains the integer target.

Output Format

Return a JSON array of arrays, where each inner array represents a unique pair of numbers that sum up to the target. The pairs should be sorted lexicographically (i.e., by the first element, then by the second element if the first elements are equal). If no such pairs exist, return an empty JSON array.

Examples

Example 1:

Input:
nums = [1, 2, 3, 4, 5]
target = 7

Output:
[[2, 5], [3, 4]]

Explanation:
The pairs (2, 5) and (3, 4) add up to 7. The output is sorted lexicographically.

Example 2:

Input:
nums = [1, 1, 2, 2, 3, 3]
target = 4

Output:
[[1, 3], [2, 2]]

Explanation:
The unique pairs that add up to 4 are (1, 3) and (2, 2).  Duplicates within the input array are handled to produce unique pairs in the output.

Example 3:

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

Output:
[[-1, 7], [1, 5]]

Explanation:
The pairs (-1, 7) and (1, 5) add up to 6.

Examples

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

Constraints

  • 1 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9