Skip to content

Files

Latest commit

 

History

History
34 lines (24 loc) · 2.23 KB

File metadata and controls

34 lines (24 loc) · 2.23 KB

Two Sums easy #javascript #blind75

by Pawan Kumar @jsartisan

Take the Challenge

Given an array of numbers nums and a number target, implement a function that finds two different positions i and j in the array where the sum of their values equals the target (i.e. nums[i] + nums[j] = target).

The input is guaranteed to contain exactly one valid solution where i and j are different indices.

Return the indices in ascending order (smaller index before larger index).

Constraints:

  • 2 ≤ nums.length ≤ 1000
  • -10,000,000 ≤ nums[i] ≤ 10,000,000
  • -10,000,000 ≤ target ≤ 10,000,000

Example Usage:

// Example 1
const nums1 = [3, 4, 5, 6];
const target1 = 7;
console.log(twoSum(nums1, target1)); // Output: [0, 1]

// Example 2
const nums2 = [4, 5, 6];
const target2 = 10;
console.log(twoSum(nums2, target2)); // Output: [0, 2]

// Example 3
const nums3 = [5, 5];
const target3 = 10;
console.log(twoSum(nums3, target3)); // Output: [0, 1]

Back Share your Solutions Check out Solutions