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

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

Example 1

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

Why

`pairWithTargetSum` returns `[0, 8]` for this input.

Example 2

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

Why

`pairWithTargetSum` returns `[4, 5]` 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.