Skip to content
RED SAGSkills
easy15 ptsarrayshash-map

Pair With Target Sum

You are given an array of integers nums and an integer target. Exactly one pair of different positions adds up to target.

Return the two positions as [i, j] with i < j.

Example 1

Input: nums = [3, 8, 11, 4], target = 12
Output: [1, 3]
Explanation: nums[1] + nums[3] = 8 + 4 = 12.

Example 2

Input: nums = [5, 5], target = 10
Output: [0, 1]

Constraints

  • 2 <= nums.length <= 10^5
  • -10^9 <= nums[i], target <= 10^9
  • Exactly one valid pair exists.
Hint 1

For each number, the partner you need is target - nums[j].

Hint 2

Store numbers you have already seen in a map from value to index so you can look the partner up in O(1).

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Game Developer, Automation Test Engineer, Blockchain Developer

Run checks your code on 3 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.