forked from akshitagit/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindTriplet.js
More file actions
44 lines (35 loc) · 1.02 KB
/
findTriplet.js
File metadata and controls
44 lines (35 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<script>
// Javascript program to find a triplet
// returns true if there is triplet with sum equal
// to 'sum' present in A[]. Also, prints the triplet
function find3Numbers(A, arr_size, sum)
{
let l, r;
// Fix the first element as A[i]
for (let i = 0; i < arr_size - 2; i++)
{
// Fix the second element as A[j]
for (let j = i + 1; j < arr_size - 1; j++)
{
// Now look for the third number
for (let k = j + 1; k < arr_size; k++)
{
if (A[i] + A[j] + A[k] == sum)
{
document.write("Triplet is " + A[i] +
", " + A[j] + ", " + A[k]);
return true;
}
}
}
}
// If we reach here, then no triplet was found
return false;
}
/* Driver code */
let A = [ 1, 4, 45, 6, 10, 8 ];
let sum = 22;
let arr_size = A.length;
find3Numbers(A, arr_size, sum);
// This code is contributed by Mayank Tyagi
</script>