From b5ca4b12e8b33a70a34e7277b8d7edd0bbb5faf9 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 17 Oct 2020 20:06:20 +0530 Subject: [PATCH] Added Mini-Max sum Algorithm --- README.md | 1 + src/algorithms/math/mini-max/MiniMax.js | 27 +++++++++++++++++++ src/algorithms/math/mini-max/Readme.md | 12 +++++++++ .../math/mini-max/__test__/miniMax.test.js | 13 +++++++++ 4 files changed, 53 insertions(+) create mode 100644 src/algorithms/math/mini-max/MiniMax.js create mode 100644 src/algorithms/math/mini-max/Readme.md create mode 100644 src/algorithms/math/mini-max/__test__/miniMax.test.js diff --git a/README.md b/README.md index 2a2c50ae..fd2e52ff 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ a set of rules that precisely define a sequence of operations. * `B` [Pascal's Triangle](src/algorithms/math/pascal-triangle) * `B` [Complex Number](src/algorithms/math/complex-number) - complex numbers and basic operations with them * `B` [Radian & Degree](src/algorithms/math/radian) - radians to degree and backwards conversion + * `B` [Mini-Max Sum](src/algorithms/math/mini-max-sum) * `B` [Fast Powering](src/algorithms/math/fast-powering) * `A` [Integer Partition](src/algorithms/math/integer-partition) * `A` [Square Root](src/algorithms/math/square-root) - Newton's method diff --git a/src/algorithms/math/mini-max/MiniMax.js b/src/algorithms/math/mini-max/MiniMax.js new file mode 100644 index 00000000..f6a1c47e --- /dev/null +++ b/src/algorithms/math/mini-max/MiniMax.js @@ -0,0 +1,27 @@ + +/** + * + * @param numbers - array of numbers + * @return [number - Max, number - Min] + */ +export default function miniMaxSum(numbers) { + if (!Array.isArray(numbers) || numbers.length === 0) { + return [null, null]; + } + + let min = numbers[0]; + let max = numbers[0]; + let sum = numbers[0]; + + for (let i = 1; i < numbers.length; i += 1) { + sum += numbers[i]; + + if (numbers[i] > max) { + max = numbers[i]; + } else if (numbers[i] < min) { + min = numbers[i]; + } + } + + return [sum - max, sum - min]; + } \ No newline at end of file diff --git a/src/algorithms/math/mini-max/Readme.md b/src/algorithms/math/mini-max/Readme.md new file mode 100644 index 00000000..2a2fc116 --- /dev/null +++ b/src/algorithms/math/mini-max/Readme.md @@ -0,0 +1,12 @@ + +# Mini-Max Sum + +Given an array of five positive integers, find the minimum and maximum values that +can be calculated by summing exactly four of the array integers. Then return the +respective minimum and maximum values as a array of numbers + + +``` +[1, 2, 3, 4, 5] = [10, 14] +[1, 3, 5, 7, 9] = [16, 24] +``` \ No newline at end of file diff --git a/src/algorithms/math/mini-max/__test__/miniMax.test.js b/src/algorithms/math/mini-max/__test__/miniMax.test.js new file mode 100644 index 00000000..e0059876 --- /dev/null +++ b/src/algorithms/math/mini-max/__test__/miniMax.test.js @@ -0,0 +1,13 @@ +import miniMaxSum from '../miniMaxSum'; + +describe('miniMaxSum', () => { + it('should calculate miniMaxSum', () => { + expect(miniMaxSum([1, 3, 5, 7, 9])).toStrictEqual([16, 24]); + expect(miniMaxSum([1, 2, 3, 4, 5])).toStrictEqual([10, 14]); + expect(miniMaxSum([-1, -2, 3, 4, 5])).toStrictEqual([4, 11]); + expect(miniMaxSum([0, 1, 2, 3, 4, 5])).toStrictEqual([10, 15]); + expect(miniMaxSum([])).toStrictEqual([null, null]); + expect(miniMaxSum(12)).toStrictEqual([null, null]); + expect(miniMaxSum(null)).toStrictEqual([null, null]); + }); +}); \ No newline at end of file