Rates TodayNIFTY25,670-0.83%SENSEX83,630.7-0.83%Gold 24K ₹11,320.24/g down186.46Silver ₹134.77/g down2.72
InsightforGeeks

Two sum

EasyArrays · JavaScript

The task

Write twoSum(nums, target) that returns the indices of the two numbers in nums that add up to target, as an array [i, j] with i < j.

Every input has exactly one pair that works, and you may not use the same element twice.

Examples

  • twoSum([2, 7, 11, 15], 9) returns [0,1]

Your code runs in your browser, in a separate worker, and is never sent to us. Edit the function, then press Run tests. Your work is saved here as you type.

JavaScript

Stuck?