Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement