Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 820f8e9

Browse files
jhonDoe15raklaptudirm
andauthored
merge: Create firstRelativeMaxPointInArray.js (#772)
* Create first_relative_max_point_in_array.js go over randomly generated array and print first spike or maximum point index in it runs in O(log(n)) * rename file to match requested casing * add comments I prefer SOLID standards so that's why didn't add them at first but due to the repository requirements was needed to be added * remove template unrelated comments * Update equals check to match JavaScript standards * create file skafolding and adjust filename to reflect main function * using newer node version * add tests * add last line as empty line * style changes * move algorithm tests to test folder * revert to old package lock file * chore: add ending line feed Co-authored-by: Rak Laptudirm <raklaptudirm@gmail.com>
1 parent 1cef191 commit 820f8e9

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* [LocalMaxima](https://www.geeksforgeeks.org/find-indices-of-all-local-maxima-and-local-minima-in-an-array/) is an algorithm to find relative bigger numbers compared to their neighbors
3+
*
4+
* Notes:
5+
* - works by using divide and conquer
6+
* - the function gets the array A with n Real numbersand returns the local max point index (if more than one exists return the first one)
7+
*
8+
* @complexity: O(log(n)) (on average )
9+
* @complexity: O(log(n)) (worst case)
10+
* @flow
11+
*/
12+
const findMaxPointIndex = (array, rangeStartIndex, rangeEndIndex, originalLength) => {
13+
// find index range middle point
14+
const middleIndex = rangeStartIndex + parseInt((rangeEndIndex - rangeStartIndex) / 2)
15+
16+
// handle array bounds
17+
if ((middleIndex === 0 || array[middleIndex - 1] <= array[middleIndex]) &&
18+
(middleIndex === originalLength - 1 || array[middleIndex + 1] <= array[middleIndex])) {
19+
return middleIndex
20+
} else if (middleIndex > 0 && array[middleIndex - 1] > array[middleIndex]) {
21+
return findMaxPointIndex(array, rangeStartIndex, (middleIndex - 1), originalLength)
22+
} else {
23+
// regular local max
24+
return findMaxPointIndex(array, (middleIndex + 1), rangeEndIndex, originalLength)
25+
}
26+
}
27+
28+
const LocalMaximomPoint = (A) => findMaxPointIndex(A, 0, A.length - 1, A.length)
29+
30+
export { LocalMaximomPoint }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { LocalMaximomPoint } from '../LocalMaximomPoint'
2+
3+
describe('LocalMaximomPoint tests', () => {
4+
it('test boundry maximom points - last element', () => {
5+
const Array = [1, 2, 3, 4, 5, 6, 12]
6+
expect(LocalMaximomPoint(Array)).toEqual(6)
7+
})
8+
9+
it('test boundry maximom points - first element', () => {
10+
const Array2 = [13, 6, 5, 4, 3, 2, 1]
11+
expect(LocalMaximomPoint(Array2)).toEqual(0)
12+
})
13+
14+
it('test boundry maximom points - should find first maximom point from the top', () => {
15+
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
16+
const Array = [13, 2, 3, 4, 5, 6, 12]
17+
expect(LocalMaximomPoint(Array)).toEqual(6)
18+
})
19+
20+
it('test inner points - second element', () => {
21+
const Array2 = [13, 16, 5, 4, 3, 2, 1]
22+
expect(LocalMaximomPoint(Array2)).toEqual(1)
23+
})
24+
25+
it('test inner points - element some where in the middle', () => {
26+
const Array2 = [13, 16, 5, 41, 3, 2, 1]
27+
expect(LocalMaximomPoint(Array2)).toEqual(3)
28+
})
29+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /