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

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

Example 1

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

Why

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

Example 2

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

Why

`pairWithTargetSum` returns `[34, 40]` 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.