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

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

Example 1

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

Why

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

Example 2

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

Why

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