Skip to content

Commit 78e3cb8

Browse files
committed
Implement FindMaxRecursion in JS
1 parent 8767d0b commit 78e3cb8

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

Maths/FindMaxRecursion.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @function findMaxRecursion
3+
* @description This algorithm will find the maximum value of a array of numbers.
4+
*
5+
* @param {Integer[]} arr Array of numbers
6+
* @param {Integer} left Index of the first element
7+
* @param {Integer} right Index of the last element
8+
*
9+
* @return {Integer} Maximum value of the array
10+
*
11+
* @see [Maximum value](https://en.wikipedia.org/wiki/Maximum_value)
12+
*
13+
* @example findMaxRecursion([1, 2, 4, 5]) = 5
14+
* @example findMaxRecursion([10, 40, 100, 20]) = 100
15+
* @example findMaxRecursion([-1, -2, -4, -5]) = -1
16+
*/
17+
function findMaxRecursion(arr, left, right) {
18+
const len = arr.length
19+
20+
if (len === 0 || !arr) return undefined
21+
22+
if (left >= len || left < -len || right >= len || right < -len)
23+
throw new Error('Index out of range')
24+
25+
if (left === right) return arr[left]
26+
27+
// left + right shifted is the middle index
28+
const mid = (left + right) >> 1
29+
30+
// Find the maximum of left and right
31+
const leftMax = findMaxRecursion(arr, left, mid)
32+
const rightMax = findMaxRecursion(arr, mid + 1, right)
33+
34+
// Return the maximum
35+
return Math.max(leftMax, rightMax)
36+
}
37+
38+
export { findMaxRecursion }

Maths/test/FindMaxRecursion.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { findMaxRecursion } from '../FindMaxRecursion'
2+
3+
describe('Test findMaxRecursion function', () => {
4+
const positiveAndNegativeArray = [1, 2, 4, 5, -1, -2, -4, -5]
5+
const positiveAndNegativeArray1 = [10, 40, 100, 20, -10, -40, -100, -20]
6+
7+
const positiveArray = [1, 2, 4, 5]
8+
const positiveArray1 = [10, 40, 100, 20]
9+
10+
const negativeArray = [-1, -2, -4, -5]
11+
const negativeArray1 = [-10, -40, -100, -20]
12+
13+
const zeroArray = [0, 0, 0, 0]
14+
const emptyArray = []
15+
16+
it('Testing with positive arrays', () => {
17+
expect(findMaxRecursion(positiveArray, 0, positiveArray.length - 1)).toBe(5)
18+
expect(findMaxRecursion(positiveArray1, 0, positiveArray1.length - 1)).toBe(
19+
100
20+
)
21+
})
22+
23+
it('Testing with negative arrays', () => {
24+
expect(findMaxRecursion(negativeArray, 0, negativeArray.length - 1)).toBe(
25+
-1
26+
)
27+
expect(findMaxRecursion(negativeArray1, 0, negativeArray1.length - 1)).toBe(
28+
-10
29+
)
30+
})
31+
32+
it('Testing with positive and negative arrays', () => {
33+
expect(
34+
findMaxRecursion(
35+
positiveAndNegativeArray,
36+
0,
37+
positiveAndNegativeArray.length - 1
38+
)
39+
).toBe(5)
40+
expect(
41+
findMaxRecursion(
42+
positiveAndNegativeArray1,
43+
0,
44+
positiveAndNegativeArray1.length - 1
45+
)
46+
).toBe(100)
47+
})
48+
49+
it('Testing with zero arrays', () => {
50+
expect(findMaxRecursion(zeroArray, 0, zeroArray.length - 1)).toBe(0)
51+
})
52+
53+
it('Testing with empty arrays', () => {
54+
expect(findMaxRecursion(emptyArray, 0, emptyArray.length - 1)).toBe(
55+
undefined
56+
)
57+
})
58+
})

0 commit comments

Comments
 (0)