DSA Trainer
← All problems

Two Sum

Hash MapEasy

Step 1 of 8 · Understand

1. Understand the problem

You are given a list of numbers and a target. Find two numbers in the list that add up to the target, and return their positions (indexes).

Input

An array of integers `nums` and an integer `target`. Each input has exactly one valid answer, and you can't use the same element twice.

Output

An array of two indexes `[i, j]` (in any order) such that `nums[i] + nums[j] === target`.

Examples

input → nums = [2, 7, 11, 15], target = 9
output → [0, 1]
nums[0] + nums[1] = 2 + 7 = 9, so the answer is [0, 1].
input → nums = [3, 2, 4], target = 6
output → [1, 2]
nums[1] + nums[2] = 2 + 4 = 6.

Write your solution above and hit Run tests. Results and tips appear here.