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

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

Example 1

pairWithTargetSum()
Input
nums = [4, 19, 36, 15, 26, 8, 7, 37, 18, 23, 14, 2, 13, 31, 1, 28, 21, 10], target = 30
Output
[0, 4]

Why

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

Example 2

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

Why

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