-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathNextGreater.java
43 lines (40 loc) · 1.48 KB
/
NextGreater.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
/* Approach for solving
This code finds the next greater element for each element in the array.
It uses a stack data structure to keep track of the indices of the elements in the array.
The loop starts from the end of the array and for each element, the code checks
if there's any element in the stack with a value greater than the current element.
If there is, that element is the next greater element for the current element.
If not, the current element has no next greater element, so -1 is stored.
Finally, the index of the current element is pushed onto the stack.
The process repeats for all elements in the array.
The result is stored in the "nxtGre" array, and finally, it's printed out.
*/
package Java_practice.Stack;
import java.util.*;
public class NextGreater {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int arr[]=new int[size];
for(int i=0;i<size;i++){
arr[i]=sc.nextInt();
}
Stack<Integer> s=new Stack<>();
int nxtGre[]=new int[size];
for(int i=size-1;i>=0;i--){
while(!s.isEmpty() && arr[s.peek()]<=arr[i]){
s.pop();
}
if(s.isEmpty()){
nxtGre[i]=-1;
}else{
nxtGre[i]=arr[s.peek()];
}
s.push(i);
}
for(int j=0;j<nxtGre.length;j++){
System.out.print(nxtGre[j]+" ");
}
System.out.println();
}
}