Easy · Arrays

Two sum

Given an integer array nums (not necessarily sorted) and an integer target, return the indices of the two different elements whose values add up to target, in any order; exactly one such pair exists.

Examples

Example 1

[2,7,11,15] → 9

Output: [0,1]

Example 2

[3,2,4] → 6

Output: [1,2]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Arrays problems