Two Sum

Easy
Arrays Array Hash Table

Given an array of integers nums and an integer target, return the indices of the two numbers that add up to target.

You may assume each input has exactly one solution, and you may not use the same element twice.

Return the answer as two space-separated indices.

Constraints

2 ≤ nums.length ≤ 10^4 -10^9 ≤ nums[i] ≤ 10^9

Examples

Example 1:
Input: 2 7 11 15 9
Output: 0 1
Explanation: nums[0]+nums[1] = 2+7 = 9
Example 2:
Input: 3 2 4 6
Output: 1 2
Explanation: nums[1]+nums[2] = 2+4 = 6

Hints

A brute force approach checks every pair. Can you do better?
Use a hash map to store values you've seen.
▲ Console

Install Talent Arabia

Get instant access to jobs and career tools on your device.