Sum of Two Integers
easySave
ArrayHash Table
Given an array of integers and a target number, return the indices of the two numbers such that they add up to the target. Each input would have exactly one solution, and you may not use the same element twice.
Example 1
Input
[ [ 2, 7, 11, 15 ], 9 ]
Output
[0, 1]
Explanation. The numbers at indices 0 and 1 are 2 and 7, which add up to 9.
Example 2
Input
[ [ 3, 2, 4 ], 6 ]
Output
[1, 2]
Explanation. The numbers at indices 1 and 2 are 2 and 4, which add up to 6.
Example 3
Input
[ [ 3, 3 ], 6 ]
Output
[0, 1]
Explanation. The numbers at indices 0 and 1 are both 3 and they add up to 6.
Follow-up: Can you think of a solution that has a time complexity lower than O(n^2)?
Constraints:
- Each input array will contain at least two integers. - There is exactly one solution for each input. - You may not use the same element twice in your calculation.
- Views
- 3