Skip to content

Commit 21a8511

Browse files
Exortionsappgurueu
andauthored
merge: Find max recursion (TheAlgorithms#1010)
* add yt video association algorithm * Implement FindMaxRecursion in JS * Delete file in other PR * Fix requested changes * Add requested changes * Delete YoutubeVideoAssociation.test.js * Deduplicate comment * Remove trailing spaces Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
1 parent cbc669a commit 21a8511

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

Maths/FindMaxRecursion.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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) {
21+
return undefined
22+
}
23+
24+
if (left >= len || left < -len || right >= len || right < -len) {
25+
throw new Error('Index out of range')
26+
}
27+
28+
if (left === right) {
29+
return arr[left]
30+
}
31+
32+
// n >> m is equivalent to floor(n / pow(2, m)), floor(n / 2) in this case, which is the mid index
33+
const mid = (left + right) >> 1
34+
35+
const leftMax = findMaxRecursion(arr, left, mid)
36+
const rightMax = findMaxRecursion(arr, mid + 1, right)
37+
38+
// Return the maximum
39+
return Math.max(leftMax, rightMax)
40+
}
41+
42+
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy