📘
snesjhon
  • You Should Know
  • Personal
    • Blogs
      • I'll never complain about web tooling again.
      • vscodevim
  • JavaScript
    • Closures
      • Closure Q1
    • Values
      • Primitive vs Reference
      • Accessing by value and reference
    • Function
      • call, apply, bind
      • Pass by value
      • Different types of Scopes
      • Context vs Scope
      • Parse Time Vs Run Time
    • Hosting
      • Are Let & Const Hoisted?
      • Hoisting Q1
      • Hoisting Q2
    • Standard Objects
      • Math
        • Math.log10
        • Math.pow()
    • Array
      • Apply
      • Slice vs Splice vs Split
    • This
      • This - Q1
    • TypeScript
      • as const
    • FAQs
      • Modulo Operator
      • Timeout
      • Declarative vs Imperative
      • ++i vs. i++
    • Interview Questions
  • react
    • FAQs
  • Ruby
    • Debugging
    • Symbols
    • Intro
  • Algorithms
    • Sliding Window
      • minSubArrayLen
      • maxSubArraySum
      • findLongestSubstring
    • Frequency Counter
      • sameFrequency
    • Recursion
      • nestedEvenSum
      • flatten
      • Reverse a String
      • Fibonnacci
    • Searching
      • overlappingIntervals
      • twoStringSearch
      • binarySearch
    • Sort - Elementary
      • Selection Sort
      • Bubble Sort
      • Insertion Sort
      • quickSort
    • Sort - Intermediate
      • Radix Sort
      • Merge Sort
    • FAQs
  • Data Structures
    • Breadth First Search
    • Linked Lists
      • Singly Linked Lists
    • FAQs
  • Code Challenges
    • LeetCode
      • removeDuplicates
    • Hacker Rank
      • twoSums
      • Sock Merchant
    • Hacker Rank - Medium
      • New Year Chaos
  • Databases
    • SQL
Powered by GitBook
On this page
  1. Code Challenges
  2. Hacker Rank

twoSums

Last updated 4 years ago

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.

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

Input: [3,2,4], 6
Output: [1,2]
var twoSum = function(nums, target) {
  // BRUTE
  // for(var i = 0; i <= nums.length - 1; i++){
  //     for(var j = 1; j <= nums.length - 1; j++){
  //         if(nums[i] + nums[j] === target){
  //             return [i, j]
  //         }
  //     }
  // }
  // return result;

  // TWO POINTER
  // let sortedNums = nums.sort((a, b) => a - b);
  // let i = 0;
  // let j = nums.length - 1;
  // while(i < j){
  //     let result = nums[i] + nums[j];
  //     if(result === target){
  //         return [i, j]
  //     }
  //     if(result > target){
  //         j--;
  //     }
  //     if(result < target){
  //         i++;
  //     }
  // }
  // return -1;

  // HASHMAP
  let available = {};
  for (var i = 0; i <= nums.length - 1; i++) {
    const val = target - nums[i]; //?
    if (typeof available[val] !== "undefined") {
      return [available[val], i];
    } else {
      available[nums[i]] = i;
    }
  }
  return -1;
};

Resources

https://www.youtube.com/watch?v=I58Kw-9tHgY