P1 · 2 seconds · 256 MB

Two Sum

Given an array of integers `nums` and an integer `target`, return the indices of the two numbers that add up to `target`. There is exactly one solution, and you may not use the same element twice. Return the two indices in ascending order.

function twoSum(nums, target) → [i, j]

CONSTRAINTS

  • 2 ≤ nums.length ≤ 10⁴
  • -10⁹ ≤ nums[i], target ≤ 10⁹
  • Exactly one valid pair
Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]
2 + 7 = 9
Input: nums = [3, 2, 4], target = 6
Output: [1, 2]
1
2
3
4
5
6
7
8

Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.