-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathNextGreaterNumber.java
64 lines (52 loc) · 1.59 KB
/
NextGreaterNumber.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
package com.geeksforgeeks.array;
import java.util.Stack;
/**
* [4, 5, 2, 25}
* Element NGE
* 4 --> 5
* 5 --> 25
* 2 --> 25
* 25 --> -1
*/
public class NextGreaterNumber {
public static void main(String[] args) {
// printNextgreaterNumber(new int[]{4, 5, 2, 25});
printNextGreaterElement(new int[]{13, 7, 6, 12});
}
public static void printNextgreaterNumber(int[] arr) {
Stack<Integer> stack = new Stack<>();
// Push first item
stack.push(arr[0]);
int nextElement = arr[1];
int counter = 1;
while (!stack.isEmpty()) {
while (stack.peek() < nextElement) {
System.out.println(stack.pop() + " :: " + nextElement);
if (stack.isEmpty())
break;
}
counter = counter + 1;
stack.push(nextElement);
if (counter >= arr.length) {
break;
}
nextElement = arr[counter];
}
while (!stack.isEmpty()) {
System.out.println(stack.pop() + " :: " + -1);
}
}
public static void printNextGreaterElement(int[] arr) {
Stack<Integer> stack = new Stack<>();
stack.push(arr[0]);
for (int i = 1; i < arr.length; i++) {
while (!stack.isEmpty() && arr[i] > stack.peek()) {
System.out.println(stack.pop() + " :: " + arr[i]);
}
stack.push(arr[i]);
}
while (!stack.isEmpty()) {
System.out.println(stack.pop() + " :: " + -1);
}
}
}