Skip to content

Commit c5c31c8

Browse files
fixed checkstyle violation in NearestElement.java
1 parent cddd83b commit c5c31c8

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed
Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,78 @@
1-
package datastructures.stacks;
1+
package com.thealgorithms.datastructures.stacks;
22

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

65
/**
76
* The {@code NearestElement} class provides static utility methods to find the nearest greater or smaller elements
87
* to the left or right of each element in an integer array using stack-based algorithms.
9-
*
8+
*
109
* <p>Each method runs in O(n) time complexity by maintaining a monotonic stack:
1110
* <ul>
1211
* <li>{@code nearestGreaterToRight}: Finds the nearest greater element to the right of each element.</li>
1312
* <li>{@code nearestGreaterToLeft}: Finds the nearest greater element to the left of each element.</li>
1413
* <li>{@code nearestSmallerToRight}: Finds the nearest smaller element to the right of each element.</li>
1514
* <li>{@code nearestSmallerToLeft}: Finds the nearest smaller element to the left of each element.</li>
1615
* </ul>
17-
* If no such element exists for a position, -1 is returned at that index.
1816
*/
19-
public class NearestElement {
17+
public final class NearestElement {
18+
19+
private NearestElement() {
20+
throw new UnsupportedOperationException("Utility class");
21+
}
22+
2023
public static int[] nearestGreaterToRight(int[] arr) {
2124
int n = arr.length;
22-
int[] res = new int[n];
23-
Stack<Integer> st = new Stack<>();
25+
int[] result = new int[n];
26+
Stack<Integer> stack = new Stack<>();
2427
for (int i = n - 1; i >= 0; i--) {
25-
while (!st.isEmpty() && st.peek() <= arr[i]) st.pop();
26-
res[i] = st.isEmpty() ? -1 : st.peek();
27-
st.push(arr[i]);
28+
while (!stack.isEmpty() && stack.peek() <= arr[i]) {
29+
stack.pop();
30+
}
31+
result[i] = stack.isEmpty() ? -1 : stack.peek();
32+
stack.push(arr[i]);
2833
}
29-
return res;
34+
return result;
3035
}
3136

3237
public static int[] nearestGreaterToLeft(int[] arr) {
3338
int n = arr.length;
34-
int[] res = new int[n];
35-
Stack<Integer> st = new Stack<>();
39+
int[] result = new int[n];
40+
Stack<Integer> stack = new Stack<>();
3641
for (int i = 0; i < n; i++) {
37-
while (!st.isEmpty() && st.peek() <= arr[i]) st.pop();
38-
res[i] = st.isEmpty() ? -1 : st.peek();
39-
st.push(arr[i]);
42+
while (!stack.isEmpty() && stack.peek() <= arr[i]) {
43+
stack.pop();
44+
}
45+
result[i] = stack.isEmpty() ? -1 : stack.peek();
46+
stack.push(arr[i]);
4047
}
41-
return res;
48+
return result;
4249
}
4350

4451
public static int[] nearestSmallerToRight(int[] arr) {
4552
int n = arr.length;
46-
int[] res = new int[n];
47-
Stack<Integer> st = new Stack<>();
53+
int[] result = new int[n];
54+
Stack<Integer> stack = new Stack<>();
4855
for (int i = n - 1; i >= 0; i--) {
49-
while (!st.isEmpty() && st.peek() >= arr[i]) st.pop();
50-
res[i] = st.isEmpty() ? -1 : st.peek();
51-
st.push(arr[i]);
56+
while (!stack.isEmpty() && stack.peek() >= arr[i]) {
57+
stack.pop();
58+
}
59+
result[i] = stack.isEmpty() ? -1 : stack.peek();
60+
stack.push(arr[i]);
5261
}
53-
return res;
62+
return result;
5463
}
5564

5665
public static int[] nearestSmallerToLeft(int[] arr) {
5766
int n = arr.length;
58-
int[] res = new int[n];
59-
Stack<Integer> st = new Stack<>();
67+
int[] result = new int[n];
68+
Stack<Integer> stack = new Stack<>();
6069
for (int i = 0; i < n; i++) {
61-
while (!st.isEmpty() && st.peek() >= arr[i]) st.pop();
62-
res[i] = st.isEmpty() ? -1 : st.peek();
63-
st.push(arr[i]);
70+
while (!stack.isEmpty() && stack.peek() >= arr[i]) {
71+
stack.pop();
72+
}
73+
result[i] = stack.isEmpty() ? -1 : stack.peek();
74+
stack.push(arr[i]);
6475
}
65-
return res;
66-
}
67-
68-
public static void main(String[] args) {
69-
int[] arr = {4, 5, 2, 10, 8};
70-
System.out.println("Nearest Greater to Right: " + Arrays.toString(nearestGreaterToRight(arr)));
71-
System.out.println("Nearest Greater to Left: " + Arrays.toString(nearestGreaterToLeft(arr)));
72-
System.out.println("Nearest Smaller to Right: " + Arrays.toString(nearestSmallerToRight(arr)));
73-
System.out.println("Nearest Smaller to Left: " + Arrays.toString(nearestSmallerToLeft(arr)));
76+
return result;
7477
}
7578
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
66
import org.junit.jupiter.api.Test;
77

8-
import datastructures.stacks.NearestElement;
8+
import com.thealgorithms.datastructures.stacks.NearestElement;
99

1010
class NearestElementTest {
1111

0 commit comments

Comments
 (0)