Skip to content

Commit 6c7d8da

Browse files
committed
Fix NearestElement.java & add comprehensive tests and formatted with clang for stack algorithms
1 parent 4c6f15a commit 6c7d8da

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

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

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

55
/**
6-
* Implementation of classic stack-based algorithms:
6+
* Implements classic stack-based algorithms to find nearest elements.
7+
*
8+
* Algorithms included:
79
* 1. Nearest Greater to Right
810
* 2. Nearest Greater to Left
911
* 3. Nearest Smaller to Right
1012
* 4. Nearest Smaller to Left
11-
*
12-
* These algorithms are fundamental for technical interviews and array-stack problems.
1313
*/
1414
public final class NearestElement {
15-
1615
// Private constructor to prevent instantiation
17-
private NearestElement() {
18-
}
16+
private NearestElement() {}
1917

18+
/** Finds the nearest greater element to the right for each element in the array. */
2019
public static int[] nearestGreaterToRight(int[] arr) {
21-
if (arr == null) throw new IllegalArgumentException("Input array cannot be null");
20+
if (arr == null) {
21+
throw new IllegalArgumentException("Input array cannot be null");
22+
}
2223

2324
int n = arr.length;
2425
int[] result = new int[n];
@@ -31,11 +32,15 @@ public static int[] nearestGreaterToRight(int[] arr) {
3132
result[i] = stack.isEmpty() ? -1 : stack.peek();
3233
stack.push(arr[i]);
3334
}
35+
3436
return result;
3537
}
3638

39+
/** Finds the nearest greater element to the left for each element in the array. */
3740
public static int[] nearestGreaterToLeft(int[] arr) {
38-
if (arr == null) throw new IllegalArgumentException("Input array cannot be null");
41+
if (arr == null) {
42+
throw new IllegalArgumentException("Input array cannot be null");
43+
}
3944

4045
int n = arr.length;
4146
int[] result = new int[n];
@@ -48,11 +53,15 @@ public static int[] nearestGreaterToLeft(int[] arr) {
4853
result[i] = stack.isEmpty() ? -1 : stack.peek();
4954
stack.push(arr[i]);
5055
}
56+
5157
return result;
5258
}
5359

60+
/** Finds the nearest smaller element to the right for each element in the array. */
5461
public static int[] nearestSmallerToRight(int[] arr) {
55-
if (arr == null) throw new IllegalArgumentException("Input array cannot be null");
62+
if (arr == null) {
63+
throw new IllegalArgumentException("Input array cannot be null");
64+
}
5665

5766
int n = arr.length;
5867
int[] result = new int[n];
@@ -65,11 +74,15 @@ public static int[] nearestSmallerToRight(int[] arr) {
6574
result[i] = stack.isEmpty() ? -1 : stack.peek();
6675
stack.push(arr[i]);
6776
}
77+
6878
return result;
6979
}
7080

81+
/** Finds the nearest smaller element to the left for each element in the array. */
7182
public static int[] nearestSmallerToLeft(int[] arr) {
72-
if (arr == null) throw new IllegalArgumentException("Input array cannot be null");
83+
if (arr == null) {
84+
throw new IllegalArgumentException("Input array cannot be null");
85+
}
7386

7487
int n = arr.length;
7588
int[] result = new int[n];
@@ -82,6 +95,7 @@ public static int[] nearestSmallerToLeft(int[] arr) {
8295
result[i] = stack.isEmpty() ? -1 : stack.peek();
8396
stack.push(arr[i]);
8497
}
98+
8599
return result;
86100
}
87-
}
101+
}

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ void testNearestGreaterToRight(int[] input, int[] expected) {
1818
}
1919

2020
static Stream<Arguments> provideGreaterToRightTestCases() {
21-
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[] {})
25-
);
21+
return Stream.of(Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {5, 10, 10, -1, -1}), Arguments.of(new int[] {5}, new int[] {-1}), Arguments.of(new int[] {}, new int[] {}));
2622
}
2723

2824
@ParameterizedTest
@@ -32,11 +28,7 @@ void testNearestGreaterToLeft(int[] input, int[] expected) {
3228
}
3329

3430
static Stream<Arguments> provideGreaterToLeftTestCases() {
35-
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[] {})
39-
);
31+
return Stream.of(Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, -1, 5, -1, 10}), Arguments.of(new int[] {5}, new int[] {-1}), Arguments.of(new int[] {}, new int[] {}));
4032
}
4133

4234
@ParameterizedTest
@@ -46,11 +38,7 @@ void testNearestSmallerToRight(int[] input, int[] expected) {
4638
}
4739

4840
static Stream<Arguments> provideSmallerToRightTestCases() {
49-
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[] {})
53-
);
41+
return Stream.of(Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {2, 2, -1, 8, -1}), Arguments.of(new int[] {5}, new int[] {-1}), Arguments.of(new int[] {}, new int[] {}));
5442
}
5543

5644
@ParameterizedTest
@@ -60,11 +48,7 @@ void testNearestSmallerToLeft(int[] input, int[] expected) {
6048
}
6149

6250
static Stream<Arguments> provideSmallerToLeftTestCases() {
63-
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[] {})
67-
);
51+
return Stream.of(Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, 4, -1, 2, 2}), Arguments.of(new int[] {5}, new int[] {-1}), Arguments.of(new int[] {}, new int[] {}));
6852
}
6953

7054
@Test
@@ -74,4 +58,4 @@ void testNullInput() {
7458
assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestSmallerToRight(null));
7559
assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestSmallerToLeft(null));
7660
}
77-
}
61+
}

0 commit comments

Comments
 (0)