Skip to main content
Back to problems
Leetcode
Easy
Arrays
Hash Maps
Amazon
Two Sum

Find two distinct numbers in an array whose sum equals a target value.

Acceptance 100%
Problem Statement

Given an integer array nums and an integer target, find the indices of the two distinct elements such that their values add up to target.

You may assume that exactly one valid pair exists for the standard problem variant. Return the indices in any order.

Input Format

  • nums: an array of integers
  • target: an integer sum to search for

Output Format

Return the indices of the two elements whose values sum to target. The indices may be returned in any order.

Constraints

  • The two indices must be different
  • Each input is assumed to have a valid answer for the standard variant
  • Aim for better than O(n2)O(n^2) time if possible
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

[0,1]

Explanation

nums[0] + nums[1] = 2 + 7 = 9. Indices can be returned in any order.

Example 2

Input

nums = [3,2,4], target = 6

Output

[1,2]

Explanation

2 + 4 = 6, so the indices 1 and 2 form a valid answer.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.