Two Sum
Given an array and a target, return the indices of the two numbers that add up to the target. The naive double-loop is O(nΒ²); a hash map of "value β index" gets it to O(n): for each number, check if its complement was already seen.
Your task
Write twoSum(nums, target) returning a 2-element array of indices
[i, j] (i before j). Return [] if no pair exists.
β‘ 45 XP
solution.jsRuns in your browser
Loading editorβ¦
Tests3 to pass
[2,7,11,15], 9 β [0,1]
[3,2,4], 6 β [1,2]
no pair β []
Run your code to check these.