-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_Greater.java
More file actions
55 lines (47 loc) · 1013 Bytes
/
Stack_Greater.java
File metadata and controls
55 lines (47 loc) · 1013 Bytes
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
package Daily_Problem;
import java.util.Stack;
public class Stack_Greater {
static Stack<Integer> ori=new Stack<>();
static Stack<Integer> ans=new Stack<>();
static Stack<Integer> stack=new Stack<>();
public static void greater(int current)
{
if(stack.isEmpty())
{
ans.push(-1);
return;
}
int num1=stack.pop();
if(stack.isEmpty())
{
ans.push(-1);
return;
}
int num2=stack.peek();
if(num1 < num2)
{
ans.push(num2);
}
else
{
ans.push(-1);
}
greater(current+1);
}
public static void main(String[] args) {
ori.push(6);
ori.push(8);
ori.push(0);
ori.push(1);
ori.push(3);
int s=ori.size();
for(int i=0;i<s;i++)
{
stack.push(ori.pop());
}
System.out.println("Original Stack : "+ stack);
int size=stack.size();
greater(0);
System.out.println(" Stack : "+ ans);
}
}