Indices of a Pair That Sums to Target (Set 11)

Easy

Given an array of integers nums and a target, return the indices of one pair whose values add up to target. Return the two indices as [i, j] — the order does not matter.

Every input has exactly one valid pair.

Indices of a Pair That Sums to Target (Set 11) — the data. This set runs 4 cases: nums holds arrays of 43–47 items, with values from 1 to 40; target ranges over 17–51. The hidden cases are the largest of the set.

Example 1

pairWithTargetSum()
Input
nums = [32, 17, 7, 18, 36, 22, 31, 29, 1, 12, 15, 3, 16, 10, 14, 34, 19, 13, 21, 27, 5, 33, 9, 2, 8, 30, 11, 28, 39, 4, 26, 35, 20, 23, 37, 40, 24, 25, 38, 6, 1, 1, 1, 1, 1, 1], target = 46
Output
[1, 7]

Why

`pairWithTargetSum` returns `[1, 7]` for this input.

Example 2

pairWithTargetSum()
Input
nums = [21, 6, 2, 5, 9, 24, 28, 18, 38, 10, 3, 35, 19, 1, 30, 40, 34, 23, 25, 7, 37, 39, 20, 8, 13, 32, 16, 4, 26, 22, 17, 11, 15, 33, 14, 29, 31, 36, 27, 12, 1, 1, 1], target = 17
Output
[9, 19]

Why

`pairWithTargetSum` returns `[9, 19]` for this input.
Constraints
2 <= nums.length <= 10^4
-10^4 <= nums[i], target <= 10^4
Exactly one valid pair exists.
Every case in this set must pass: the first two are visible on Run, the last two unlock on Submit.

Topics

ArraysHash Table

Implement pairWithTargetSum in the editor and return the answer — printing it is never graded.