-
Notifications
You must be signed in to change notification settings - Fork 1
/
6thJuly(V).java
67 lines (45 loc) · 1.79 KB
/
6thJuly(V).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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.Arrays;
import java.util.Stack;
public class NextGreaterElement {
// naive ------------
static void approach1(int[] arr, int n){
for(int i =0; i<n; i++){
int j;
for(j = i+1; j<n; j++){
if(arr[j]>arr[i]){
System.out.print(arr[j]+",");
break;
}
}
if(j==n){ // if it reached the end of the array
System.out.print(-1+",");
}
}
}
// this question is also done by stack
static void approach2(int[] arr, int n){
Stack<Integer> st = new Stack<>(); // Java's predefined stack // peek returns the top element
int output[] = new int[n];
for(int i =n-1 ;i>=0;i--){ // run the loop in reverse
while(!st.isEmpty() && st.peek()<=arr[i]){ // if stack is not empty and top element is less than current iterating element
st.pop(); // then pop the top element
}
output[i]=st.empty()?-1:st.peek(); // if stack is empty and then append -1 to result array ... if stack is not empty then append top element of stack
st.push(arr[i]); // after appending ...push the current element to stack
}
//-------------printing ----------------
for(int i : output){
System.out.print(i+",");
}
}
public static void main(String[] args) {
int[] arr = {5,10,15,8,9,12,6};
int n = arr.length;
approach1(arr, n);
System.out.println();
approach2(arr, n);
}
}
// ====================== Output ====================
// 10,15,-1,9,12,-1,-1,
// 10,15,-1,9,12,-1,-1,