Skip to content
Decoder

Two Sum

Easy

Problem

Given a list of integers nums and a number target, find the two different positions whose values add up to target. Return their indices as a list in increasing order.

Example 1

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

Example 2

Input
nums = [3, 2, 4], target = 6
Output
[1, 2]
Why
2 + 4

Example 3

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

You may not use the same index twice.

Constraints

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • Exactly one valid answer exists.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.