-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.js
More file actions
37 lines (29 loc) · 1.07 KB
/
array.js
File metadata and controls
37 lines (29 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Returns an array that contains the indexes of the greaters elements * near of each value of the parameter array.
*
* @param array An array of numbers.
* @returns An array of indexes (numbers).
*/
const nearestGreater = (array) => {
const arraySize = array.length
return array.map((value, index) => {
let preIndex = index-1
let postIndex = index+1
while(preIndex >= 0 || postIndex < arraySize){
if(preIndex >= 0 && array[preIndex] > value) return preIndex
if(postIndex < arraySize && array[postIndex] > value) return postIndex
preIndex--
postIndex++
}
return -1
})
}
const largestAdjacentProduct = (inputArray) => {
const arraySize = inputArray.length
let largestProduct = Number.MIN_SAFE_INTEGER
for(let pointer = 0; pointer < arraySize-1; pointer++){
largestProduct = Math.max(inputArray[pointer]*inputArray[pointer+1], largestProduct)
}
return largestProduct
}
module.exports = {nearestGreater, largestAdjacentProduct}