Two Sum

Problem 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.

Input / Output

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

Constraints

  • 2 ≤ n ≤ 10^4
  • Exactly one solution exists; the two indices must be distinct. Return them in any order.

Example

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