Two Sum / Hash Table

Problem Given an array of integers and a target value, return the indices of the two numbers that add up to the target. Exactly one such pair is guaranteed to exist, and you may not use the same element twice.

Input / Output

  • Input: an integer array nums and an integer target.
  • Output: a pair of indices [i, j] with nums[i] + nums[j] == target and i != j.

Constraints

  • 2 ≤ n ≤ 10^4.
  • Exactly one valid answer exists.
  • The same element may not be reused.

Example

  • nums = [2,7,11,15], target = 9 → [0,1] because nums[0] + nums[1] == 9.
  • nums = [3,2,4], target = 6 → [1,2].
added …
LeaderboardSalaryAccount