forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNextGreaterElement.java
63 lines (55 loc) · 1.91 KB
/
NextGreaterElement.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
package com.rampatra.arrays;
import com.rampatra.base.LinkedStack;
import com.rampatra.base.Stack;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 8/26/15
* @time: 12:35 PM
*/
public class NextGreaterElement {
/**
* Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element
* for an element x is the first greater element on the right side of x in array. Elements for which
* no greater element exist, consider next greater element as -1.
*
* @param a
* @return
*/
public static void nextGreaterElements(int[] a) {
int i = 0;
Stack<Integer> stack = new LinkedStack<>(); // used to store elements whose NGE is yet to be determined
for (; i < a.length - 1; i++) {
stack.push(a[i]);
while (!stack.isEmpty()) {
Integer pop = stack.pop();
if (pop < a[i + 1]) { // NGE found for popped element
System.out.println(pop + "->" + a[i + 1]);
} else {
stack.push(pop); // NGE still not found for popped element, so push it again
break;
}
}
}
// no NGE for elements left in stack
while (!stack.isEmpty()) {
System.out.println(stack.pop() + "->" + -1);
}
// no NGE for last element
System.out.println(a[i] + "->" + -1);
}
public static void main(String[] args) {
int[] ar = new int[]{4, 5, 2, 25};
nextGreaterElements(ar);
System.out.println("=========");
ar = new int[]{11, 13, 21, 3};
nextGreaterElements(ar);
System.out.println("=========");
ar = new int[]{1, 5, 3, 4, 2, 0, 11};
nextGreaterElements(ar);
System.out.println("=========");
ar = new int[]{3, 6, 8, 2, 1, 5, 12, 4, 9};
nextGreaterElements(ar);
}
}