SHARE
TWEET

Untitled

a guest Jul 17th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const xSumPairs = (nums, x) => {
  2.   const pairs = [];
  3.  
  4.   for(let i = 0; i < nums.length; i++) {
  5.     const currNum = nums[i];
  6.  
  7.     for(let j = i + 1; j < nums.length; j++) {
  8.       if (j === i) continue;
  9.  
  10.       const nextNum = nums[j];
  11.  
  12.       if (currNum + nextNum === x) pairs.push([i, j]);
  13.     }
  14.   }
  15.  
  16.   return pairs;
  17. }
  18.  
  19. const betterXSumPairs = (nums, x) => {
  20.   const numsAndIndicies = {};
  21.   const pairs = [];
  22.  
  23.   for(let i = 0; i < nums.length; i++) {
  24.     const currNum = nums[i];
  25.     const missingNum = x - currNum;
  26.     if (numsAndIndicies[missingNum] !== undefined) {
  27.       pairs.push([i, numsAndIndicies[missingNum]])
  28.     } else {
  29.       numsAndIndicies[currNum] = i;
  30.     }
  31.   }
  32.  
  33.   return pairs;
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top