-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathNext_Greater_Element.java
45 lines (33 loc) · 1.27 KB
/
Next_Greater_Element.java
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
public class Next_Greater_Element {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
//Initialising hashmap
HashMap<Integer, Integer> map = new HashMap<>();
//Initialising stack
Stack<Integer> stack = new Stack<Integer>();
//Iterating over the array
for(int i=0;i<nums2.length;i++){
//Terminating condition
while(!stack.isEmpty()){
//If the top of stack is of lesser value than the array element
if(stack.peek()<nums2[i]){
//Adding to the hashmap
map.put(stack.pop(), nums2[i]);
}
else{
break;
}
}
//Pushing on the stack
stack.push(nums2[i]);
}
//Initializing array
int[] arr=new int[nums1.length];
//Iterating the array
for(int i=0;i<nums1.length;i++){
//FILLING THE NEW ARRAY WITH HASHMAP VALUES
arr[i] = map.getOrDefault(nums1[i], -1);
}
// returning the answer array
return arr;
}
}