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

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

Example 1

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

Why

`pairWithTargetSum` returns `[2, 12]` for this input.

Example 2

pairWithTargetSum()
Input
nums = [7, 30, 2, 6, 18, 16, 38, 13, 8, 39, 29, 5, 26, 22, 36, 12, 27, 37, 14, 40, 20, 10, 33, 21, 31, 11, 3, 35], target = 38
Output
[1, 8]

Why

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