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

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

Example 1

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

Why

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

Example 2

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

Why

`pairWithTargetSum` returns `[1, 2]` 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.