Skip to content

Commit cddd83b

Browse files
Add print statements to all NearestElementTest methods
1 parent e6cb96f commit cddd83b

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package datastructures.stacks;
2+
3+
import java.util.Arrays;
4+
import java.util.Stack;
5+
6+
/**
7+
* The {@code NearestElement} class provides static utility methods to find the nearest greater or smaller elements
8+
* to the left or right of each element in an integer array using stack-based algorithms.
9+
*
10+
* <p>Each method runs in O(n) time complexity by maintaining a monotonic stack:
11+
* <ul>
12+
* <li>{@code nearestGreaterToRight}: Finds the nearest greater element to the right of each element.</li>
13+
* <li>{@code nearestGreaterToLeft}: Finds the nearest greater element to the left of each element.</li>
14+
* <li>{@code nearestSmallerToRight}: Finds the nearest smaller element to the right of each element.</li>
15+
* <li>{@code nearestSmallerToLeft}: Finds the nearest smaller element to the left of each element.</li>
16+
* </ul>
17+
* If no such element exists for a position, -1 is returned at that index.
18+
*/
19+
public class NearestElement {
20+
public static int[] nearestGreaterToRight(int[] arr) {
21+
int n = arr.length;
22+
int[] res = new int[n];
23+
Stack<Integer> st = new Stack<>();
24+
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+
}
29+
return res;
30+
}
31+
32+
public static int[] nearestGreaterToLeft(int[] arr) {
33+
int n = arr.length;
34+
int[] res = new int[n];
35+
Stack<Integer> st = new Stack<>();
36+
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]);
40+
}
41+
return res;
42+
}
43+
44+
public static int[] nearestSmallerToRight(int[] arr) {
45+
int n = arr.length;
46+
int[] res = new int[n];
47+
Stack<Integer> st = new Stack<>();
48+
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]);
52+
}
53+
return res;
54+
}
55+
56+
public static int[] nearestSmallerToLeft(int[] arr) {
57+
int n = arr.length;
58+
int[] res = new int[n];
59+
Stack<Integer> st = new Stack<>();
60+
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]);
64+
}
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)));
74+
}
75+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.Arrays;
4+
5+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
6+
import org.junit.jupiter.api.Test;
7+
8+
import datastructures.stacks.NearestElement;
9+
10+
class NearestElementTest {
11+
12+
@Test
13+
void testNearestGreaterToRight() {
14+
int[] arr = {4, 5, 2, 10, 8};
15+
int[] result = NearestElement.nearestGreaterToRight(arr);
16+
System.out.println("nearestGreaterToRight: " + Arrays.toString(result));
17+
int[] expected = {5, 10, 10, -1, -1};
18+
assertArrayEquals(expected, result);
19+
}
20+
21+
@Test
22+
void testNearestGreaterToLeft() {
23+
int[] arr = {4, 5, 2, 10, 8};
24+
int[] result = NearestElement.nearestGreaterToLeft(arr);
25+
System.out.println("nearestGreaterToLeft: " + Arrays.toString(result));
26+
int[] expected = {-1, -1, 5, -1, 10};
27+
assertArrayEquals(expected, result);
28+
}
29+
30+
@Test
31+
void testNearestSmallerToRight() {
32+
int[] arr = {4, 5, 2, 10, 8};
33+
int[] result = NearestElement.nearestSmallerToRight(arr);
34+
System.out.println("nearestSmallerToRight: " + Arrays.toString(result));
35+
int[] expected = {2, 2, -1, 8, -1};
36+
assertArrayEquals(expected, result);
37+
}
38+
39+
@Test
40+
void testNearestSmallerToLeft() {
41+
int[] arr = {4, 5, 2, 10, 8};
42+
int[] result = NearestElement.nearestSmallerToLeft(arr);
43+
System.out.println("nearestSmallerToLeft: " + Arrays.toString(result));
44+
int[] expected = {-1, 4, -1, 2, 2};
45+
assertArrayEquals(expected, result);
46+
}
47+
}

0 commit comments

Comments
 (0)