Algorithm that checks whether the sum of any two items of an ordered array is equal to a number yielding wrong results

Asked

Viewed 72 times

-2

This pairSum algorithm should find if the sum of any two items in an array arr is equal to a given number target:

    function pairSum(arr, target){

    for (var i=0; i<arr.length;){

        for (var j = arr.length-1; j>i;){

            if (i=j) {return false} else {

                if (arr[i] + arr[j] === target){ //we have our case

                    return true;

                } else if (arr[i]+arr[j] < target) { //we don't have our case, but we know the last item (right index = j) needs to stay on the calculation, so we only change i

                    i++;

                } else {

                    j--; //we still don't have our case, but we know the last item (right index = j) is not part of the solution, so we skip it to the left, and we keep the left item (index = i)

                }
           }; 
        };

    };
    return false;

};

//invocation // current result // desired result
console.log( pairSum( [3,4,6,8,9,11], 14) ); //false // true because 11+3=14
console.log( pairSum( [3,4,6,8,9,11], 8)  ); //false // false
console.log( pairSum( [3,4,4,8,9,11], 8)  ); //false // true because 4+4=8
console.log( pairSum( [3,4,6,8,9,11], 16) ); //false // false
console.log( pairSum( [3,4,6,8,9,11], 20) ); //false // true because 11+9=20
  • 3

    And where is the question?

  • Just looking up, you forgot one = here if (i=j) and the for'are incomplete because they lack i++ and j--

2 answers

0

Two errors were complicating the solution of the algorithm:

if (i=j) {return false} else {

should be:

if (i===j) {return false} else {

This causes the algorithm to correctly evaluate the positive cases, but becomes an infinite loop in the negative cases. This can be easily fixed by transforming:

} else if (arr[i]+arr[j] < target) {

in:

} else if (arr[i]+arr[j] <= target) {

The final code therefore remains:

    function pairSum(arr, target){

    for (var i=0; i<arr.length;){

        for (var j = arr.length-1; j>=i;){

            if (i===j) {return false} else {

                if (arr[i] + arr[j] === target){ //we have our case

                    return true;

                } else if (arr[i]+arr[j] <= target) { //we don't have our case, but we know the last item (right index = j) needs to stay on the calculation, so we only change i

                    i++;

                } else {

                    j--; //we still don't have our case, but we know the last item (right index = j) is not part of the solution, so we skip it to the left, and we keep the left item (index = i)

                }
           }; 
        };

    };
    return false;

};

console.log( pairSum( [3,4,6,8,9,11], 14) ); //true 
console.log( pairSum( [3,4,6,8,9,11], 8)  ); //false 
console.log( pairSum( [3,4,4,8,9,11], 8)  ); //true 
console.log( pairSum( [3,4,6,8,9,11], 16) ); //false
console.log( pairSum( [3,4,6,8,9,11], 20) ); //true

0

function pairSum(target, number){
  for(var i in target){
    for(var j in target){
      if(i == j) continue;
      if(target[i] + target[j] == number) return true;
    }
  }
  return false;
}

console.log( pairSum( [3,4,6,8,9,11], 14) ); //true because 11+3=14
console.log( pairSum( [3,4,6,8,9,11], 8)  ); //false
console.log( pairSum( [3,4,4,8,9,11], 8)  ); //true because 4+4=8
console.log( pairSum( [3,4,6,8,9,11], 16) ); //false
console.log( pairSum( [3,4,6,8,9,11], 20) ); //true because 11+9=20

  • 1

    This does not explain where the problem is, could edit and detail.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.