1
1
package com .thealgorithms .stacks ;
2
2
3
- import java .util .Arrays ;
4
3
import java .util .Stack ;
5
4
6
- /*
7
- Given an array "input" you need to print the first greater element for each element.
8
- For a given element x of an array, the Next greater element of that element is the
9
- first greater element to the right side of it. If no such element is present print -1.
10
-
11
- Example
12
- input = { 2, 7, 3, 5, 4, 6, 8 };
13
- At i = 0
14
- Next greater element between (1 to n) is 7
15
- At i = 1
16
- Next greater element between (2 to n) is 8
17
- At i = 2
18
- Next greater element between (3 to n) is 5
19
- At i = 3
20
- Next greater element between (4 to n) is 6
21
- At i = 4
22
- Next greater element between (5 to n) is 6
23
- At i = 5
24
- Next greater element between (6 to n) is 8
25
- At i = 6
26
- Next greater element between (6 to n) is -1
27
-
28
- result : [7, 8, 5, 6, 6, 8, -1]
29
-
30
- 1. If the stack is empty Push an element in the stack.
31
- 2. If the stack is not empty:
32
- a. compare the top element of the stack with next.
33
- b. If next is greater than the top element, Pop element from the stack.
34
- next is the next greater element for the popped element.
35
- c. Keep popping from the stack while the popped element is smaller
36
- than next. next becomes the next greater element for all such
37
- popped elements.
38
- d. Finally, push the next in the stack.
39
-
40
- 3. If elements are left in stack after completing while loop then their Next greater element is
41
- -1.
5
+ /**
6
+ * Utility class to find the next greater element for each element in a given integer array.
7
+ *
8
+ * <p>The next greater element for an element x is the first greater element on the right side of x in the array.
9
+ * If no such element exists, the result will contain 0 for that position.</p>
10
+ *
11
+ * <p>Example:</p>
12
+ * <pre>
13
+ * Input: {2, 7, 3, 5, 4, 6, 8}
14
+ * Output: {7, 0, 5, 6, 6, 8, 0}
15
+ * </pre>
42
16
*/
43
-
44
17
public final class NextGreaterElement {
45
18
private NextGreaterElement () {
46
19
}
47
20
21
+ /**
22
+ * Finds the next greater element for each element in the given array.
23
+ *
24
+ * @param array the input array of integers
25
+ * @return an array where each element is replaced by the next greater element on the right side in the input array,
26
+ * or 0 if there is no greater element.
27
+ * @throws IllegalArgumentException if the input array is null
28
+ */
48
29
public static int [] findNextGreaterElements (int [] array ) {
49
30
if (array == null ) {
50
- return array ;
31
+ throw new IllegalArgumentException ( "Input array cannot be null" ) ;
51
32
}
52
33
53
34
int [] result = new int [array .length ];
@@ -62,10 +43,4 @@ public static int[] findNextGreaterElements(int[] array) {
62
43
63
44
return result ;
64
45
}
65
-
66
- public static void main (String [] args ) {
67
- int [] input = {2 , 7 , 3 , 5 , 4 , 6 , 8 };
68
- int [] result = findNextGreaterElements (input );
69
- System .out .println (Arrays .toString (result ));
70
- }
71
46
}
0 commit comments