-1

I am trying to solve this problem in JavaScript: Given an array and a value in JavaScript, find if there is a triplet in array whose sum is equal to the given value. If there is such a triplet present in array, then print the triplet and return true. Else return false.

Now, I wrote some code, and for some reason, it's not working properly. Here is the code:

A = [1, 4, 45, 6, 10, 8];
sum = 15;
x = A.length;


function find3Numbers(A, x, sum) {
for (i=0; i<(x-2); i++) {
for (j=i+1; j<(x-1); j++) {
for (k=j+1; x; k++) {
   if (A[i] + A[j] + A[k] == sum) {
     console.log(A[i]);
     console.log(A[j]);
     console.log(A[k]);
     return true
    }
   return false
    }
  }
 }
} 

 console.log(find3Numbers(A, x, sum));

Now when I run the code, I get a "false" message. Any idea, why is that?

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Coder48
  • 41
  • 6
  • Because `return` returns out of a function, not a for loop. In the case that the first case does not match, you return false. The function stops – Taplar Dec 11 '20 at 19:54
  • You need to return false after the end of the outer most for loop – Taplar Dec 11 '20 at 19:56
  • Does this answer your question? [Does return stop a loop?](https://stackoverflow.com/questions/11714503/does-return-stop-a-loop) – Heretic Monkey Dec 11 '20 at 19:58

2 Answers2

2

You immediately return false if the first triplet you try does not match, when you should only do so after all loops have finished.

A = [1, 4, 45, 6, 10, 8];
sum = 15;
x = A.length;


function find3Numbers(A, x, sum) {
  for (i = 0; i < (x - 2); i++) {
    for (j = i + 1; j < (x - 1); j++) {
      for (k = j + 1; x; k++) {
        if (A[i] + A[j] + A[k] == sum) {
          console.log(A[i]);
          console.log(A[j]);
          console.log(A[k]);
          return true
        }
      }
    }
  }
  return false;
}

console.log(find3Numbers(A, x, sum));
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
1

Consider sorting the array beforehand for O(n^2) time complexity rather than O(n^3).

Note: O(nlog(n) + n^2) is asymptotically the same as O(n^2).

const find3Numbers = (nums, nums_length, target) => {
    nums.sort((a, b) => a - b);
    let i = 0;
    while (i < nums_length - 2) {
        let j = i + 1;
        let k = nums_length - 1;
        while (j < k) {
            curr_sum = nums[i] + nums[j] + nums[k];
            if (curr_sum < target) {
                j++;
                while (j < k && nums[j] == nums[j - 1]) {
                    j++;
                }
            } else if (curr_sum > target) {
                k--;
                while (j < k && nums[k] == nums[k + 1]) {
                    k--;
                }
            } else {
                return [nums[i], nums[j], nums[k]];
            }
        }
        i++;
        while (i < nums_length - 2 && nums[i] == nums[i - 1]) {
            i++;
        }
    }
    return [-1, -1, -1];
}


const A = [1, 4, 45, 6, 10, 8];
const x = A.length;
const sum = 15;
console.log(find3Numbers(A, x, sum));
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40