Skip to main content
All Visualizers
#LC 1•Arrays & Hashing•Easy

Two Sum

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

Understand the problem

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

Example 1

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

Output: [0, 1]

The values at indices zero and one add to nine.

Constraints

  • There is exactly one valid answer.
  • You may not use the same element twice.
Think first

For each number, what information would let you find its partner quickly?

Pattern tags:Hash map lookupComplement search
Time: O(n)Space: O(n)
Step 1 of 7 • Use Left/Right arrows or Space to control