Find the Pair With a Target Sum
Problem statement
Given an integer array nums and an integer target, find the two numbers at two different positions that add up to target. Exactly one such pair of positions exists.
Return the two numbers as [a, b] with a <= b. A position cannot be used twice, but the same value may appear at two positions.
Function
findPairWithSum(nums: int[], target: int) → int[]Examples
Example 1
nums = [8,-3,14,6,11]target = 19return = [8,11]8 + 11 = 19, and no other two positions add up to 19. The smaller value comes first: [8, 11].
Example 2
nums = [7,2,7,-4]target = 14return = [7,7]The two 7s are at different positions, so they form the pair: [7, 7].
Example 3
nums = [5,1,9]target = 10return = [1,9]5 + 5 would need two 5s, but there is only one, and a position cannot be used twice. The pair is 1 + 9.
Constraints
2 <= nums.length <= 10^4-10^9 <= nums[i] <= 10^9-2 * 10^9 <= target <= 2 * 10^9- exactly one pair of positions
i < jhasnums[i] + nums[j] == target