diff --git a/Week-6/Assignment-6.1/index.js b/Week-6/Assignment-6.1/index.js new file mode 100644 index 0000000..376a88b --- /dev/null +++ b/Week-6/Assignment-6.1/index.js @@ -0,0 +1,10 @@ +function longestSubArray(arr) { + let a = arr[0]; + for (let i = 1; i < arr.length; i++) { + arr[i] = Math.max(arr[i], arr[i] + arr[i - 1]); + a = Math.max(a,arr[i]) + } + return a +} +console.log(longestSubArray([1,2,3,4,-10])); +console.log(longestSubArray([-2,1,-3,4,-1,2,1,-5,4])); diff --git a/Week-6/Assignment-6.3/index.js b/Week-6/Assignment-6.3/index.js new file mode 100644 index 0000000..2db9679 --- /dev/null +++ b/Week-6/Assignment-6.3/index.js @@ -0,0 +1,6 @@ +//const arr = [0,1,0]; +const arr = [0, 2, 1, 2, 0]; +arr.sort(function (a, b) { + return a - b; +}); +console.log(arr); diff --git a/Week-6/Assignment-6.5/index.js b/Week-6/Assignment-6.5/index.js new file mode 100644 index 0000000..8577d46 --- /dev/null +++ b/Week-6/Assignment-6.5/index.js @@ -0,0 +1,12 @@ +function arrDifference(arr, B) { + for (i = 0; i < arr.length - 1; i++) { + for (j = i + 1; j < arr.length; j++) { + if (arr[j] - arr[i] == B) { + return 1; + } + } + } + return 0; +} +console.log(arrDifference([5, 10, 3, 2, 50, 80], 78)); +console.log(arrDifference([-10,20], 30)); 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));