From 41ddee7074a44a0fada60b39c838cad334968d2f Mon Sep 17 00:00:00 2001 From: devcer Date: Thu, 14 Oct 2021 21:10:34 +0530 Subject: [PATCH 1/3] Added Maximum product of 3 numbers in an array --- Dynamic-Programming/MaxProductOfThree.js | 37 +++++++++++++++++++ .../tests/MaxProductOfThree.test.js | 15 ++++++++ 2 files changed, 52 insertions(+) create mode 100644 Dynamic-Programming/MaxProductOfThree.js create mode 100644 Dynamic-Programming/tests/MaxProductOfThree.test.js diff --git a/Dynamic-Programming/MaxProductOfThree.js b/Dynamic-Programming/MaxProductOfThree.js new file mode 100644 index 000000000..24a2ad6e9 --- /dev/null +++ b/Dynamic-Programming/MaxProductOfThree.js @@ -0,0 +1,37 @@ +/** + * Given an array of numbers, return the maximum product + * of 3 numbers from the array + * @param {number[]} arrayItems + * @returns number + */ +export function maxProductOfThree(arrayItems) { + // if size is less than 3, no triplet exists + let n = arrayItems.length + if (n < 3) return -1 + let max1 = arrayItems[0], + max2 = -1, + max3 = -1, + min1 = arrayItems[0], + min2 = -1 + for (let i = 1; i < n; i++) { + if (arrayItems[i] > max1) { + max3 = max2 + max2 = max1 + max1 = arrayItems[i] + } else if (max2 === -1 || arrayItems[i] > max2) { + max3 = max2 + max2 = arrayItems[i] + } else if (max3 === -1 || arrayItems[i] > max3) { + max3 = arrayItems[i] + } + if (arrayItems[i] < min1) { + min2 = min1 + min1 = arrayItems[i] + } else if (min2 === -1 || arrayItems[i] < min2) { + min2 = arrayItems[i] + } + } + let prod1 = max1 * max2 * max3, + prod2 = max1 * min1 * min2 + return Math.max(prod1, prod2) +} diff --git a/Dynamic-Programming/tests/MaxProductOfThree.test.js b/Dynamic-Programming/tests/MaxProductOfThree.test.js new file mode 100644 index 000000000..41861bac1 --- /dev/null +++ b/Dynamic-Programming/tests/MaxProductOfThree.test.js @@ -0,0 +1,15 @@ +import { maxProductOfThree } from '../MaxProductOfThree' + +describe('MaxProductOfThree', () => { + it('expects to return -1 for array with only 2 numbers', () => { + expect(maxProductOfThree([1, 3])).toBe(-1) + }) + + it('expects to return 300 as the maximum product', () => { + expect(maxProductOfThree([10, 6, 5, 3, 1, -10])).toBe(300) + }) + + it('expects to return 300 as the maximum product', () => { + expect(maxProductOfThree([10, -6, 5, 3, 1, -10])).toBe(600) + }) +}) From b3377bb79cb7275951f42aab0d950dbff9e14f1f Mon Sep 17 00:00:00 2001 From: devcer Date: Thu, 14 Oct 2021 21:14:48 +0530 Subject: [PATCH 2/3] added a link to explanation --- Dynamic-Programming/MaxProductOfThree.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Dynamic-Programming/MaxProductOfThree.js b/Dynamic-Programming/MaxProductOfThree.js index 24a2ad6e9..811913c69 100644 --- a/Dynamic-Programming/MaxProductOfThree.js +++ b/Dynamic-Programming/MaxProductOfThree.js @@ -1,6 +1,7 @@ /** * Given an array of numbers, return the maximum product * of 3 numbers from the array + * https://wsvincent.com/javascript-three-sum-highest-product-of-three-numbers/ * @param {number[]} arrayItems * @returns number */ From 67ec915d97a85ca17bb6c063e7b88ee1ce92206e Mon Sep 17 00:00:00 2001 From: devcer Date: Fri, 15 Oct 2021 21:49:29 +0530 Subject: [PATCH 3/3] added throwing an error when array with <3 items is passed --- Dynamic-Programming/MaxProductOfThree.js | 2 +- Dynamic-Programming/tests/MaxProductOfThree.test.js | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Dynamic-Programming/MaxProductOfThree.js b/Dynamic-Programming/MaxProductOfThree.js index 811913c69..1e7ebe3e1 100644 --- a/Dynamic-Programming/MaxProductOfThree.js +++ b/Dynamic-Programming/MaxProductOfThree.js @@ -8,7 +8,7 @@ export function maxProductOfThree(arrayItems) { // if size is less than 3, no triplet exists let n = arrayItems.length - if (n < 3) return -1 + if (n < 3) throw new Error('Triplet cannot exist with the given array') let max1 = arrayItems[0], max2 = -1, max3 = -1, diff --git a/Dynamic-Programming/tests/MaxProductOfThree.test.js b/Dynamic-Programming/tests/MaxProductOfThree.test.js index 41861bac1..32aaec542 100644 --- a/Dynamic-Programming/tests/MaxProductOfThree.test.js +++ b/Dynamic-Programming/tests/MaxProductOfThree.test.js @@ -1,8 +1,10 @@ import { maxProductOfThree } from '../MaxProductOfThree' describe('MaxProductOfThree', () => { - it('expects to return -1 for array with only 2 numbers', () => { - expect(maxProductOfThree([1, 3])).toBe(-1) + it('expects to throw error for array with only 2 numbers', () => { + expect(() => { + maxProductOfThree([1, 3]) + }).toThrow('Triplet cannot exist with the given array') }) it('expects to return 300 as the maximum product', () => {