DSA Trainer

Two Sum

Hash MapEasy

Step 1 of 8 · Understand

1. Understand the problem

Problem statement

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

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.