Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to count and print triplets from an array with sum smaller than a given value in Node.js

1 Answer

0 votes
const array = [4, 5, 2, 8, 1, 3, 8, 7, 10];
        
const size = array.length;
let sum = 11;
let count = 0;
        
for (let i = 0; i < size - 2; i++) {
    for (let j = i + 1; j < size - 1; j++) {
        for (let k = j + 1; k < size; k++) {
            if (array[i] + array[j] + array[k] < sum) {
                count++;
                console.log(count + ": " + array[i] + "," + array[j] + "," + array[k]);
            }
        }
    }
}

console.log("Number of Triplets = " + count);





/*
run:

1: 4,5,1
2: 4,2,1
3: 4,2,3
4: 4,1,3
5: 5,2,1
6: 5,2,3
7: 5,1,3
8: 2,1,3
9: 2,1,7
Number of Triplets = 9

*/

 



answered Sep 17, 2022 by avibootz
...