Pair With Target Sum
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.
[ 2, 7, 11, 15 ]
Explanation. Because nums[0] + nums[1] = 9 (which is the target), the function should return indices 0 and 1.
[ 3, 2, 4 ]
Explanation. Because nums[1] + nums[2] = 6 (which is the target), the function should return indices 1 and 2.
[ 3, 3 ]
Explanation. Because nums[0] + nums[1] = 6 (which is the target), the function should return indices 0 and 1.
Follow-up: Can you come up with an algorithm that is less than O(n^2) time complexity?
The array `nums` will not be empty and will contain at least two elements. All elements and the `target` are integers.
- Views
- 3