How to find the combination of three elements in an array whose sum is equal to N in Node.js

1 Answer

0 votes
function PrintThreeElements(arr, N) {
    const size = arr.length;
    
    for (let i = 0; i < size; i++) {
        for (let j = i + 1; j < size; j++) {
            for (let k = j + 1 ; k < size; k++) {
                if (arr[i] + arr[j] + arr[k] == N) {
                    console.log(arr[i] + " " + arr[j] + " " + arr[k]);
                    return;
                }
            }
        }
    }
}
        
const arr = [4, 3, 2, 6, 10, 5, 9, 8, 7, 12];
const N = 24;

PrintThreeElements(arr, N);




/*
run:

4 8 12

*/

 



answered Sep 7, 2022 by avibootz
...