algorythms
Arrays & Hashing
LC #1Easy

Two Sum

Arrays & Hashing
AmazonGoogleMetaAppleMicrosoftBloombergAdobe

Problem

Given an array of integers nums and an integer target, return the indices [i, j] of the two numbers that add up to target. Exactly one solution always exists; you may not use the same element twice. Example: nums=[2,7,11,15], target=9 → [0,1] because nums[0]+nums[1]=9.

arrayhash-map

Constraints

  • 2 ≤ nums.length ≤ 10⁴
  • -10⁹ ≤ nums[i] ≤ 10⁹
  • Exactly one valid answer exists

Example

Inputnums = [2, 7, 11, 15], target = 9
Output[0, 1]
Why

nums[0] + nums[1] = 2 + 7 = 9

Hints — reveal one at a time