-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathNext_Larger_Element.java
More file actions
66 lines (64 loc) · 1.35 KB
/
Next_Larger_Element.java
File metadata and controls
66 lines (64 loc) · 1.35 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
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
import java.util.*;
class Next_Larger_Element
{
Scanner sc=new Scanner(System.in);
int max=1000;
int top=-1;
int[] stack=new int[1000];
int push(int x)
{
if(top==max-1)
{
System.out.println("Stack Overflow");
}
else
{
stack[++top]=x;
}
return(top);
}
int pop(int x)
{
while(top>=0 && stack[top]<x)
{
System.out.print(x+" ");
top--;
}
stack[++top]=x;
return(top);
}
void main()
{
Next_Larger_Element obj=new Next_Larger_Element();
int t;
t=sc.nextInt();
int i,dig;
while(t-->0)
{
int topi=-1;
int n=sc.nextInt();
for(i=0;i<n;i++)
{
dig=sc.nextInt();
if(topi==-1)
{
topi=obj.push(dig);
}
else if(stack[topi]>dig)
{
topi=obj.push(dig);
}
else if(stack[topi]<dig)
{
topi=obj.pop(dig);
}
}
for(int j=0;j<=topi;j++)
{
System.out.print("-1 ");
}
obj.top=-1;
topi=-1;
}
}
}