-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNext Greater Element (NGE).js
55 lines (37 loc) · 1.18 KB
/
Next Greater Element (NGE).js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Next Greater Element (NGE) for every element in given Array
Given an array, print the Next Greater Element (NGE) for every element.
The Next greater Element for an element x is the first greater element on the right side of x in the array. Elements for which no greater element exist, consider the next greater element as -1.
Example:
Input: arr[] = [ 4 , 5 , 2 , 25 ]
Output: 4 –> 5
5 –> 25
2 –> 25
25 –> -1
Explanation: except 25 every element has an element greater than them present on the right side
Input: arr[] = [ 13 , 7, 6 , 12 ]
Output: 13 –> -1
7 –> 12
6 –> 12
12 –> -1
Explanation: 13 and 12 don’t have any element greater than them present on the right side
*/
function NextGreaterElement(arr) {
var max;
var soFarMax;
var map = new Map();
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i] < soFarMax) {
map.set(arr[i], soFarMax);
soFarMax = arr[i];
} else if (arr[i] < max) {
map.set(arr[i], max);
soFarMax = arr[i];
} else {
max = arr[i];
map.set(arr[i], -1);
}
}
console.log(map);
}
NextGreaterElement([1, 3, 4, 2]);