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

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

Example 1

pairWithTargetSum()
Input
nums = [5, 27, 10, 26, 17, 4, 11, 35, 18, 21, 31, 2, 8, 32, 22, 25, 16, 36, 7, 20, 40, 34, 19, 33], target = 67
Output
[7, 13]

Why

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

Example 2

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

Why

`pairWithTargetSum` returns `[0, 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.