← All problems

Two Sum

easy

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

You may assume each input has exactly one valid answer, and you can't use the same element twice. Return the indices in either order.

Example

Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]        // nums[0] + nums[1] = 2 + 7 = 9

Constraints

  • 2 <= nums.length <= 10^4
  • Exactly one valid pair exists.
Loading...