code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

01 Two Number Sum

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const twoNumberSum = (nums, target) => {
  let map = {}
  for (let i = 0; i < nums.length; i++) {
    const complement = target - nums[i]
    if (map[complement]) {
      return true
    } else {
      map[nums[i]] = nums[i]
    }
  }
  return false
}

console.log(twoNumberSum([3, 5, -4, 8, 11, 1, -1, 6], 10))

Created 2020-12-17T03:09:53+05:18 · Edit