From 90b12f6c525844d6f1e9dd86ddc81c8cbd594c57 Mon Sep 17 00:00:00 2001 From: bhushanhr26 <58306435+bhushanhr26@users.noreply.github.com> Date: Mon, 29 Aug 2022 22:39:34 +0530 Subject: [PATCH] The Assignment 6.6 completed --- Week-6/Assignment-6.6/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Week-6/Assignment-6.6/index.js diff --git a/Week-6/Assignment-6.6/index.js b/Week-6/Assignment-6.6/index.js new file mode 100644 index 0000000..d13ea47 --- /dev/null +++ b/Week-6/Assignment-6.6/index.js @@ -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));