EasyArrays & Hashing
Pair With Target Sum
You are given an array of integers nums and an integer target. Exactly one pair of different positions i and j holds values that add up to target.
Return the two indices as [i, j] with i < j.
Example 1
- Input:
- nums = [4, 9, 1, 6], target = 7
- Output:
- [2, 3]
nums[2] + nums[3] = 1 + 6 = 7.
Example 2
- Input:
- nums = [5, 5, 3], target = 10
- Output:
- [0, 1]
The two 5s sit at different positions, so they form a valid pair.