Skip to content

Commit c8cf302

Browse files
alxkmalxkm
and
alxkm
authored
refactor: NextGreaterElement (#5405)
* refactor: NextGreaterElement * checkstyle: fix formatting --------- Co-authored-by: alxkm <[email protected]>
1 parent 6fab70a commit c8cf302

File tree

2 files changed

+49
-45
lines changed

2 files changed

+49
-45
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,34 @@
11
package com.thealgorithms.stacks;
22

3-
import java.util.Arrays;
43
import java.util.Stack;
54

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>
4216
*/
43-
4417
public final class NextGreaterElement {
4518
private NextGreaterElement() {
4619
}
4720

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+
*/
4829
public static int[] findNextGreaterElements(int[] array) {
4930
if (array == null) {
50-
return array;
31+
throw new IllegalArgumentException("Input array cannot be null");
5132
}
5233

5334
int[] result = new int[array.length];
@@ -62,10 +43,4 @@ public static int[] findNextGreaterElements(int[] array) {
6243

6344
return result;
6445
}
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-
}
7146
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
class NextGreaterElementTest {
13+
14+
@ParameterizedTest
15+
@MethodSource("provideTestCases")
16+
void testFindNextGreaterElements(int[] input, int[] expected) {
17+
assertArrayEquals(expected, NextGreaterElement.findNextGreaterElements(input));
18+
}
19+
20+
static Stream<Arguments> provideTestCases() {
21+
return Stream.of(Arguments.of(new int[] {2, 7, 3, 5, 4, 6, 8}, new int[] {7, 8, 5, 6, 6, 8, 0}), Arguments.of(new int[] {5}, new int[] {0}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {2, 3, 4, 5, 0}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {0, 0, 0, 0, 0}),
22+
Arguments.of(new int[] {4, 5, 2, 25}, new int[] {5, 25, 25, 0}), Arguments.of(new int[] {}, new int[] {}));
23+
}
24+
25+
@Test
26+
void testNullInput() {
27+
assertThrows(IllegalArgumentException.class, () -> NextGreaterElement.findNextGreaterElements(null));
28+
}
29+
}

0 commit comments

Comments
 (0)