Target-Sum Pair with Two Pointers
Problem statement
Return true if two values at distinct array positions sum to target; otherwise return false. Input order is arbitrary and repeated values are allowed. An array with fewer than two entries returns false.
For the final approach, sort a copy and use two pointers moving inward from the smallest and largest values. The scan takes linear time after sorting.
Function
hasTargetSumPair(nums: int[], target: int) → booleanExamples
Example 1
nums = [3,1,4,2]target = 6return = trueThe values 4 and 2 occur at distinct positions and sum to 6.
Example 2
nums = [3,1]target = 6return = falseThe single occurrence of 3 cannot be reused.
Example 3
nums = [-2,0,1,3]target = 1return = trueA negative value and a positive value can form a valid pair.
Constraints
0 <= nums.length <= 5000.-1000000000 <= nums[i], target <= 1000000000.