FastPrepAll Unique Two-Sum Pairs

All Unique Two-Sum Pairs

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given an integer array nums and an integer target, return every distinct pair of values whose sum equals target.

Write each pair as [a,b] with a <= b. A pair [x,x] is valid only when x occurs at least twice. Return the pairs in increasing lexicographic order with no duplicates.

Function

allUniquePairs(nums: int[], target: int) → int[][]

Examples

Example 1

nums = [1,2,3,2,4,0]target = 4return = [[0,4],[1,3],[2,2]]

The three distinct value pairs sum to 4. Two copies of 2 make [2,2] valid.

Example 2

nums = [1,1,1]target = 2return = [[1,1]]

Repeated index choices do not create additional value-pair results.

Constraints

  • 0 <= nums.length <= 100000
  • -10^9 <= nums[i], target <= 10^9

More Microsoft problems

See Microsoft hiring insights
public int[][] allUniquePairs(int[] nums, int target) {
  // Write your code here.
}
nums[1,2,3,2,4,0]
target4
expected[[0,4],[1,3],[2,2]]
Checking account…