Arrays & Hashing
LC #1Easy
Two Sum
Arrays & Hashing
AmazonGoogleMetaAppleMicrosoftBloombergAdobeProblem
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
Input
nums = [2, 7, 11, 15], target = 9Output
[0, 1]Why
nums[0] + nums[1] = 2 + 7 = 9