Skip to content

Commit

Permalink
The Assignment 6.6 completed
Browse files Browse the repository at this point in the history
  • Loading branch information
bhushanhr26 committed Aug 29, 2022
1 parent 52fd9ed commit 90b12f6
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Week-6/Assignment-6.6/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function triplet(arr, x) {
let closestSum = Number.MAX_VALUE;

for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
for (let k = j + 1; k < arr.length; k++) {
if (Math.abs(x - closestSum) > Math.abs(x - (arr[i] + arr[j] + arr[k])))
closestSum = arr[i] + arr[j] + arr[k];
}
}
}
return closestSum;
}
console.log(triplet([-1, 2, 1, -4], 1));

0 comments on commit 90b12f6

Please sign in to comment.