Two Sum

Problem Given an array of integers and a target, return the indices of the two numbers that add up to the target. Exactly one solution exists and an element may not be used twice.

Input / Output

  • Input: array nums, integer target.
  • Output: the indices [i, j] of the two numbers summing to target.

Constraints

  • 2 ≤ n ≤ 10^4; exactly one valid pair.
  • The array is not sorted; values may be negative.

Example

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