cheetcode
Back to problems

Two Sum

Return the indices of two numbers that add up to a target.

Easy

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

You may assume that each input has exactly one solution, and you may not use the same element twice.

Examples

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

Constraints

  • 2 <= nums.length <= 10,000
  • -1,000,000 <= nums[i] <= 1,000,000

Test Cases

twoSum([2, 7, 11, 15], 9)
twoSum([3, 2, 4], 6)