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

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

Example 1

pairWithTargetSum()
Input
nums = [38, 30, 2, 24, 26, 32, 25, 13, 36, 33, 5, 10, 4], target = 18
Output
[7, 10]

Why

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

Example 2

pairWithTargetSum()
Input
nums = [14, 6, 30, 3, 4, 9, 25, 26, 33, 38, 19, 10, 1, 32, 11], target = 43
Output
[8, 11]

Why

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