Skip to content

Two Sum

Easy

Given nums: list[int] and target: int, return the indices of two distinct elements of nums whose values add to target as list[int], smaller index first. Return [] when no such pair exists. nums contains between 2 and 100000 integers, and every element and target is between -1000000000 and 1000000000 inclusive. Duplicate values are allowed. At most one pair of indices sums to target.

Examples

nums     [4, 9, 13, 20]
target   13

output   [0, 1]
nums     [-5, 8, 2, 5, 40]
target   0

output   [0, 3]
python3
Left the code editor

Console