Milan

Two Sum challenge

2022-06-25 at JavaScript category

1. Two Sum | Difficulty | Easy |

Problem:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Example:


Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Solution:

Map the numbers to their indices as we iterate, so that we can look them back up in O(1) time complexity.

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function (nums, target) {
  const indicies = {};

  for (let i = 0; i < nums.length; i++) {
    const num = nums[i];
    if (indicies[target - num] != null) {
      return [indicies[target - num], i];
    }
    indicies[num] = i;
  }
};

// Ans:

// | Your answer | Expected answer |
// | [0,1]       | [0,1]           |
Milan Sachani 🔥

Personal blog by Milan Sachani 🔥

I like to share my knowledge ❤️