Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const xSumPairs = (nums, x) => {
- const pairs = [];
- for(let i = 0; i < nums.length; i++) {
- const currNum = nums[i];
- for(let j = i + 1; j < nums.length; j++) {
- if (j === i) continue;
- const nextNum = nums[j];
- if (currNum + nextNum === x) pairs.push([i, j]);
- }
- }
- return pairs;
- }
- const betterXSumPairs = (nums, x) => {
- const numsAndIndicies = {};
- const pairs = [];
- for(let i = 0; i < nums.length; i++) {
- const currNum = nums[i];
- const missingNum = x - currNum;
- if (numsAndIndicies[missingNum] !== undefined) {
- pairs.push([i, numsAndIndicies[missingNum]])
- } else {
- numsAndIndicies[currNum] = i;
- }
- }
- return pairs;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement