ExamAdda Logo

Two Sum

Easy

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

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

Return the indices in any order.

Example 1

Input

4
2 7 11 15
9

Output

0 1

Explanation

nums[0] + nums[1] = 2 + 7 = 9, which equals the target.

Example 2

Input

3
3 2 4
6

Output

1 2

Explanation

nums[1] + nums[2] = 2 + 4 = 6.

Constraints

2 ≤ n ≤ 10⁵
-10⁹ ≤ nums[i] ≤ 10⁹
-10⁹ ≤ target ≤ 10⁹
Exactly one valid answer exists.

Hints:

Hint 1

For every element, determine what number is needed to reach the target.

Hint 2

Store previously seen numbers and their indices in a Hash Map. Before inserting the current number, check whether its complement (target − current number) already exists.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Jul 29, 2026

Last updated

Aug 6, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output