EasyTopic: Arrays & HashingExpected: O(N) Time • O(N) Space
Pair Sum Target Indices
You're given an array of integers `nums` and an integer `target`. Find the two elements that add up to exactly `target`, and return their indices. Exactly one valid pair exists for any input you'll be given, and the same element can't be used twice. You can return the two indices in either order.
Examples
Input: nums = [5, 9, 13, 2], target = 11
Output: [1, 3]
nums[1] + nums[3] == 9 + 2 == 11.
Input: nums = [1, 4, 6], target = 10
Output: [1, 2]
nums[1] + nums[2] == 4 + 6 == 10.
Constraints
- 2 <= nums.length <= 2000
- -1000 <= nums[i] <= 1000
- -2000 <= target <= 2000
- Exactly one valid pair exists.
solution.js
JavaScript (ES6+)Test Execution Results
Input:
[[5,9,13,2],11]Expected:
[1,3]Your Output:
--