1. Two Sum

Given an array of integers nums and an integer target, return the indices of the two numbers that add up to target. Exactly one valid pair exists, and you may not use the same element twice. Return the indices in any order.

Example 1

Input: nums = [4,9,1,7], target = 11
Output: [0,3]
Explanation: nums[0] + nums[3] = 4 + 7 = 11

Example 2

Input: nums = [-1,8,3,6], target = 2
Output: [0,2]
Explanation: -1 + 3 = 2

Constraints

  • 2 ≤ nums.length ≤ 10⁴
  • -10⁹ ≤ nums[i], target ≤ 10⁹
  • Exactly one answer exists
Hint 1

For each number x, the partner you need is target - x.

Hint 2

A hash map lets you check “have I already seen the partner?” in O(1).

ArrayHash Table