Two Sum

Easy · Arrays & Hashing · Array · Hash Table

Problem

You are given an array of integers nums and an integer target.

Find the two numbers in the array that add up to target, and return their positions — not the numbers themselves.

Three things the problem guarantees, which shape every solution:

Examples

Input: nums = [2,7,11,15], target = 9
Output: [0,1]

nums[0] + nums[1] == 9.

Input: nums = [3,2,4], target = 6
Output: [1,2]
Input: nums = [3,3], target = 6
Output: [0,1]

The same value may appear twice at different indices.

Constraints

Approaches

Follow-up

Can you do better than checking every pair, i.e. faster than O(n²) time?

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…