Loading practice problems…
Loading your practice workspace…
Loading code editor…
1 / 29
Sign in
Algorithms/Hash Map

Two Sum

easy20 min

Given an array of integers nums and an integer target, return the indices of the two different elements whose values add up to target.

Exactly one valid answer exists. Return the two indices in increasing order.

Example 1

Inputnums = [2, 7, 11, 15], target = 9

Output[0, 1]

nums[0] + nums[1] equals 9.

Example 2

Inputnums = [3, 2, 4], target = 6

Output[1, 2]

Constraints

  • 2 ≤ nums.length ≤ 10,000
  • Exactly one valid pair exists.
Hash MapArrays