Coding Trainer

Two Sum

EasyHash Mapk-hash-map

Problem

Two Sum

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 exactly one solution exists.
  • You may not use the same element twice.
  • Return the answer in any order.

Example:

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

Constraints:

  • 2 ≤ nums.length ≤ 10⁴
  • -10⁹ ≤ nums[i] ≤ 10⁹