Sorted Two Sum: All Index Pairs
Problem statement
Given a sorted integer array and a target, return every pair of different indices whose values sum to the target.
For this exercise, indices are one-based. Return pairs [i,j] with i < j, ordered by increasing i and then increasing j. Include every valid index pair exactly once. Equal values at different positions are different elements and may create multiple pairs; do not collapse them into one value pair. Return an empty array when there is no pair.
The one-based convention, duplicate-position semantics, ordering and bounds are explicit practice choices for the report’s multiple-result Two Sum II variant.
Function
allTwoSumPairs(numbers: int[], target: int) → int[][]Examples
Example 1
numbers = [1,1,2,2]target = 3return = [[1,3],[1,4],[2,3],[2,4]]Either position holding 1 can pair with either position holding 2, yielding [1,3], [1,4], [2,3] and [2,4]. Each is a different index pair.
Example 2
numbers = [-2,0,0,2]target = 0return = [[1,4],[2,3]]The matching one-based pairs are [1,4] and [2,3]. The two zeros occupy different positions.
Constraints
0 <= numbers.length <= 60-1000000 <= numbers[i] <= 1000000-2000000 <= target <= 2000000- numbers is sorted in nondecreasing order.