Skip to content

Commit 4c6f15a

Browse files
committed
Fix NearestElement.java & add comprehensive tests for stack algorithms
1 parent 47ed73c commit 4c6f15a

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

src/main/java/com/thealgorithms/stacks/NearestElement.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@
33
import java.util.Stack;
44

55
/**
6-
* This class provides four algorithms to find the nearest greater or smaller elements
7-
* on either side of each element in an array using a stack.
6+
* Implementation of classic stack-based algorithms:
7+
* 1. Nearest Greater to Right
8+
* 2. Nearest Greater to Left
9+
* 3. Nearest Smaller to Right
10+
* 4. Nearest Smaller to Left
811
*
9-
* <p>These algorithms are fundamental examples of the monotonic stack approach, commonly
10-
* used in competitive programming and technical interviews. Each method runs in O(n)
11-
* time complexity and O(n) space complexity.
12+
* These algorithms are fundamental for technical interviews and array-stack problems.
1213
*/
1314
public final class NearestElement {
1415

1516
// Private constructor to prevent instantiation
16-
private NearestElement() {}
17+
private NearestElement() {
18+
}
1719

18-
/**
19-
* Finds the nearest greater element to the right for each element in the array.
20-
*
21-
* @param arr the input array
22-
* @return an array containing the nearest greater element to the right for each element
23-
*/
2420
public static int[] nearestGreaterToRight(int[] arr) {
21+
if (arr == null) throw new IllegalArgumentException("Input array cannot be null");
22+
2523
int n = arr.length;
2624
int[] result = new int[n];
2725
Stack<Integer> stack = new Stack<>();
@@ -36,13 +34,9 @@ public static int[] nearestGreaterToRight(int[] arr) {
3634
return result;
3735
}
3836

39-
/**
40-
* Finds the nearest greater element to the left for each element in the array.
41-
*
42-
* @param arr the input array
43-
* @return an array containing the nearest greater element to the left for each element
44-
*/
4537
public static int[] nearestGreaterToLeft(int[] arr) {
38+
if (arr == null) throw new IllegalArgumentException("Input array cannot be null");
39+
4640
int n = arr.length;
4741
int[] result = new int[n];
4842
Stack<Integer> stack = new Stack<>();
@@ -57,13 +51,9 @@ public static int[] nearestGreaterToLeft(int[] arr) {
5751
return result;
5852
}
5953

60-
/**
61-
* Finds the nearest smaller element to the right for each element in the array.
62-
*
63-
* @param arr the input array
64-
* @return an array containing the nearest smaller element to the right for each element
65-
*/
6654
public static int[] nearestSmallerToRight(int[] arr) {
55+
if (arr == null) throw new IllegalArgumentException("Input array cannot be null");
56+
6757
int n = arr.length;
6858
int[] result = new int[n];
6959
Stack<Integer> stack = new Stack<>();
@@ -78,13 +68,9 @@ public static int[] nearestSmallerToRight(int[] arr) {
7868
return result;
7969
}
8070

81-
/**
82-
* Finds the nearest smaller element to the left for each element in the array.
83-
*
84-
* @param arr the input array
85-
* @return an array containing the nearest smaller element to the left for each element
86-
*/
8771
public static int[] nearestSmallerToLeft(int[] arr) {
72+
if (arr == null) throw new IllegalArgumentException("Input array cannot be null");
73+
8874
int n = arr.length;
8975
int[] result = new int[n];
9076
Stack<Integer> stack = new Stack<>();

src/test/java/com/thealgorithms/stacks/NearestElementTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ void testNearestGreaterToRight(int[] input, int[] expected) {
1919

2020
static Stream<Arguments> provideGreaterToRightTestCases() {
2121
return Stream.of(
22-
Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {5, 10, 10, -1, -1}),
23-
Arguments.of(new int[] {5}, new int[] {-1}),
24-
Arguments.of(new int[] {}, new int[] {})
22+
Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {5, 10, 10, -1, -1}),
23+
Arguments.of(new int[] {5}, new int[] {-1}),
24+
Arguments.of(new int[] {}, new int[] {})
2525
);
2626
}
2727

@@ -33,9 +33,9 @@ void testNearestGreaterToLeft(int[] input, int[] expected) {
3333

3434
static Stream<Arguments> provideGreaterToLeftTestCases() {
3535
return Stream.of(
36-
Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, -1, 5, -1, 10}),
37-
Arguments.of(new int[] {5}, new int[] {-1}),
38-
Arguments.of(new int[] {}, new int[] {})
36+
Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, -1, 5, -1, 10}),
37+
Arguments.of(new int[] {5}, new int[] {-1}),
38+
Arguments.of(new int[] {}, new int[] {})
3939
);
4040
}
4141

@@ -47,9 +47,9 @@ void testNearestSmallerToRight(int[] input, int[] expected) {
4747

4848
static Stream<Arguments> provideSmallerToRightTestCases() {
4949
return Stream.of(
50-
Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {2, 2, -1, 8, -1}),
51-
Arguments.of(new int[] {5}, new int[] {-1}),
52-
Arguments.of(new int[] {}, new int[] {})
50+
Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {2, 2, -1, 8, -1}),
51+
Arguments.of(new int[] {5}, new int[] {-1}),
52+
Arguments.of(new int[] {}, new int[] {})
5353
);
5454
}
5555

@@ -61,9 +61,9 @@ void testNearestSmallerToLeft(int[] input, int[] expected) {
6161

6262
static Stream<Arguments> provideSmallerToLeftTestCases() {
6363
return Stream.of(
64-
Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, 4, -1, 2, 2}),
65-
Arguments.of(new int[] {5}, new int[] {-1}),
66-
Arguments.of(new int[] {}, new int[] {})
64+
Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, 4, -1, 2, 2}),
65+
Arguments.of(new int[] {5}, new int[] {-1}),
66+
Arguments.of(new int[] {}, new int[] {})
6767
);
6868
}
6969

0 commit comments

Comments
 (0)