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

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 10) — the data. This set runs 4 cases: nums holds arrays of 41–46 items, with values from 1 to 40; target ranges over 17–67. The hidden cases are the largest of the set.

Example 1

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

Why

`pairWithTargetSum` returns `[9, 10]` for this input.

Example 2

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

Why

`pairWithTargetSum` returns `[11, 16]` 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.