Home > Net >  Finding the first pair that sums to a given integer
Finding the first pair that sums to a given integer

Time:11-12

    function sumPairs(ints, s) {
  
  let result = [];
  
  for(let i = 0; i < ints.length; i  ){
    for(let j = 0; j < ints.slice(i); j  ){ 
      (ints[i]   ints[j] === s && result.length === 0) ?  result.push(ints[i], ints[j]) : 0;
    } 
  } return result;
}
sumPairs([1, 2, 3, 4, 5, 6], 10) //returns [6, 4] instead of [4,6]?

How do I fix this so that the function returns the first pair that add up to s, the second argument? I added the result.length === 0 condition in the if statement a further update would be prevented?

CodePudding user response:

Just iterate over the array using for..of loop and use Set to achieve the desired result.

function sumPairs(ints, s) {
  const dict = new Set();

  for (let num of ints) {
    if (dict.has(s - num)) return [s - num, num];
    else dict.add(num);
  }
}
console.log(sumPairs([1, 2, 3, 4, 5, 6], 10));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Iterations:

Iteration 1 :=> num = 1 and dict is empty

dict doesn't have 9 (because of s - num i.e 10 - 1) so add num i.e 1 to dict.

Iteration 2 :=> num = 2 and dict has 1

dict doesn't have 8 (because of s - num i.e 10 - 2) so add num i.e 2 to dict

Iteration 3 :=> num = 3 and dict has 1, 2

dict doesn't have 7 (because of s - num i.e 10 - 3) so add num i.e 3 to dict

Iteration 4 :=> num = 4 and dict has 1, 2, 3

dict doesn't have 6 (because of s - num i.e 10 - 4) so add num i.e 4 to dict

Iteration 5 :=> num = 5 and dict has 1, 2, 3, 4

dict doesn't have 5 (because of s - num i.e 10 - 5) so add num i.e 5 to dict

Iteration 6 :=> num = 6 and dict has 1, 2, 3, 4, 5

dict have 4 (because of s - num i.e 10 - 6) so return [s - num, num] i.e [4, 6]

CodePudding user response:

I think you might be overcomplicating things a little. There's no need to slice() or to push(), and there's no need to keep on looping after a result has been found:

function sumPairs(ints, s) {
  for (let i = 0; i < ints.length; i  ) {
    for (let j = i; j < ints.length; j  ) {
      if (ints[i]   ints[j] == s) {
        return [ints[i], ints[j]];
      }
    }
  }
}

console.log(sumPairs([1, 2, 3, 4, 5, 6], 10));
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

As @robby-cornelissen said there's no need to slice. And there's no need for push too as you can return the result simply.

When you do ints.slice(i) you get an array from that position.
You are comparing an int within an array in the second loop.
It didn't enter the second loop until "i === 5" which is in the ints array a [6]. Notice it didn't enter the second until you get an array of one element. Keep in mind 6 == [6] and 1<[6] and 2<[6] etc

Then it goes incrementing the value of j until you get the position of j that gets the value of ints which, summed by the value of the position i results on s.

CodePudding user response:

const twoSum = (arr, target) => {
    arr.sort((a, b) => a - b);
    //double pointer -> increment, decrement accordingly
    let leftPointer = 0;
    let rightPointer = arr.length - 1;

    while (leftPointer < rightPointer) {
        if (arr[leftPointer]   arr[rightPointer] === target) {
            return [arr[leftPointer], arr[rightPointer]];
        }
        if (arr[leftPointer]   arr[rightPointer] > target) {
            rightPointer--;
        }

        if (arr[leftPointer]   arr[rightPointer] < target) {
            leftPointer  ;
        }
    }
};

console.log(twoSum([1, 2, 3, 4, 5, 6], 10));
  • Related