Skip to content

Commit

Permalink
Merge pull request #10 from pesto-students/week-6
Browse files Browse the repository at this point in the history
Week 6
  • Loading branch information
bhushanhr26 authored Dec 21, 2022
2 parents 0b72728 + 90b12f6 commit e4cb39e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Week-6/Assignment-6.1/index.js
Original file line number Diff line number Diff line change
@@ -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]));
6 changes: 6 additions & 0 deletions Week-6/Assignment-6.3/index.js
Original file line number Diff line number Diff line change
@@ -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);
12 changes: 12 additions & 0 deletions Week-6/Assignment-6.5/index.js
Original file line number Diff line number Diff line change
@@ -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));
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 e4cb39e

Please sign in to comment.