From 7c8a66fbd6009e6208287da8aa575d7755c41277 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sat, 7 Sep 2019 16:16:35 +0530 Subject: [PATCH 1/4] --update: find 2nd max in array --- src/_Problems_/find-2nd-max/index.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/_Problems_/find-2nd-max/index.js diff --git a/src/_Problems_/find-2nd-max/index.js b/src/_Problems_/find-2nd-max/index.js new file mode 100644 index 00000000..5d57c6ab --- /dev/null +++ b/src/_Problems_/find-2nd-max/index.js @@ -0,0 +1,22 @@ +/** + * You may find it easy but it's tricky for few + * Input - [9, 2, 3, 6] + * Output - 6 + */ + +function findSecondMax(arr) { + let max = arr[0]; + let max2 = 0; + + for (let el of arr) { + if (el > max) { + max2 = max; + max = el; + } + + if (el < max && el > max2) { + max2 = el; + } + } + return max2; +} From 50115f99f6c1abb6d86ae44543a598449e8f66e8 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 8 Sep 2019 12:39:47 +0530 Subject: [PATCH 2/4] --fix: change syntax, naming and comments --- .../find-2-nums-adding-to-n/index.js | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/_Problems_/find-2-nums-adding-to-n/index.js b/src/_Problems_/find-2-nums-adding-to-n/index.js index 0f6fbeb1..d35376b9 100644 --- a/src/_Problems_/find-2-nums-adding-to-n/index.js +++ b/src/_Problems_/find-2-nums-adding-to-n/index.js @@ -1,32 +1,32 @@ -// the best case using a data structure - SET -function findTwoNumsAddingToN(arr, sum) { - const nums = []; +// the best case [O(n)] using SET data structure +function findTwoNumsAddingToN(arr, number) { + const pair = []; const store = new Set(); - for (let i = 0; i < arr.length; i++) { - // check if the set contains one of the pir that sum upto given sum - if (store.has(sum - arr[i])) { - nums.push(sum - arr[i]); - nums.push(arr[i]); + for (let i = 0; i < arr.length; i += 1) { + // check if the set contains one of the pair that sum upto given number + if (store.has(number - arr[i])) { + pair.push(number - arr[i]); + pair.push(arr[i]); break; } // push the element in the set store.add(arr[i]); } - return nums.length ? nums : false; + return pair.length ? pair : false; } // the Brute force approach -function findTwoNumsAddingToN2(arr, sum) { - const nums = []; - for (let i = 0; i < arr.length; i++) { - for (let j = i + 1; j < arr.length; j++) { - if (arr[i] + arr[j] === sum) { - nums.push(arr[i], arr[j]); +function findTwoNumsAddingToN2(arr, number) { + const pair = []; + for (let i = 0; i < arr.length; i += 1) { + for (let j = i + 1; j < arr.length; j += 1) { + if (arr[i] + arr[j] === number) { + pair.push(arr[i], arr[j]); break; } } } - return nums.length ? nums : false; + return pair.length ? pair : null; } From 5aab5ccc694147f3e384ebe91119764adcf7054f Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 8 Sep 2019 12:52:01 +0530 Subject: [PATCH 3/4] --fix : comment --- src/_Problems_/find-2-nums-adding-to-n/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_Problems_/find-2-nums-adding-to-n/index.js b/src/_Problems_/find-2-nums-adding-to-n/index.js index d35376b9..b01ca1cc 100644 --- a/src/_Problems_/find-2-nums-adding-to-n/index.js +++ b/src/_Problems_/find-2-nums-adding-to-n/index.js @@ -4,7 +4,7 @@ function findTwoNumsAddingToN(arr, number) { const store = new Set(); for (let i = 0; i < arr.length; i += 1) { - // check if the set contains one of the pair that sum upto given number + // check if the set contains one of the element that sum upto the given number if (store.has(number - arr[i])) { pair.push(number - arr[i]); pair.push(arr[i]); From d1d0bd2503106ada8dd35f5a2aa9fd15267bff81 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 8 Sep 2019 23:24:05 +0530 Subject: [PATCH 4/4] --update: max2 initialization, return false if no pair found --- src/_Problems_/find-2-nums-adding-to-n/index.js | 2 +- src/_Problems_/find-2nd-max/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_Problems_/find-2-nums-adding-to-n/index.js b/src/_Problems_/find-2-nums-adding-to-n/index.js index b01ca1cc..5ba3e0a7 100644 --- a/src/_Problems_/find-2-nums-adding-to-n/index.js +++ b/src/_Problems_/find-2-nums-adding-to-n/index.js @@ -28,5 +28,5 @@ function findTwoNumsAddingToN2(arr, number) { } } - return pair.length ? pair : null; + return pair.length ? pair : false; } diff --git a/src/_Problems_/find-2nd-max/index.js b/src/_Problems_/find-2nd-max/index.js index 5d57c6ab..0cbb2433 100644 --- a/src/_Problems_/find-2nd-max/index.js +++ b/src/_Problems_/find-2nd-max/index.js @@ -6,7 +6,7 @@ function findSecondMax(arr) { let max = arr[0]; - let max2 = 0; + let max2 = Number.MIN_SAFE_INTEGER; for (let el of arr) { if (el > max) {