Skip to content

Commit fc320b5

Browse files
committed
feat: add nearest element stack algorithms with JUnit tests
1 parent 3eb521b commit fc320b5

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.thealgorithms.datastructures.stacks;
2+
3+
import java.util.Stack;
4+
import java.util.Arrays;
5+
6+
public final class NearestElement {
7+
8+
private NearestElement() {}
9+
10+
public static int[] nearestGreaterToRight(int[] arr) {
11+
int n = arr.length;
12+
int[] result = new int[n];
13+
Stack<Integer> indexStack = new Stack<>();
14+
for (int i = n - 1; i >= 0; i--) {
15+
while (!indexStack.isEmpty() && arr[i] >= arr[indexStack.peek()]) {
16+
indexStack.pop();
17+
}
18+
result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()];
19+
indexStack.push(i);
20+
}
21+
return result;
22+
}
23+
24+
public static int[] nearestGreaterToLeft(int[] arr) {
25+
int n = arr.length;
26+
int[] result = new int[n];
27+
Stack<Integer> indexStack = new Stack<>();
28+
for (int i = 0; i < n; i++) {
29+
while (!indexStack.isEmpty() && arr[i] >= arr[indexStack.peek()]) {
30+
indexStack.pop();
31+
}
32+
result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()];
33+
indexStack.push(i);
34+
}
35+
return result;
36+
}
37+
38+
public static int[] nearestSmallerToRight(int[] arr) {
39+
int n = arr.length;
40+
int[] result = new int[n];
41+
Stack<Integer> indexStack = new Stack<>();
42+
for (int i = n - 1; i >= 0; i--) {
43+
while (!indexStack.isEmpty() && arr[i] <= arr[indexStack.peek()]) {
44+
indexStack.pop();
45+
}
46+
result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()];
47+
indexStack.push(i);
48+
}
49+
return result;
50+
}
51+
52+
public static int[] nearestSmallerToLeft(int[] arr) {
53+
int n = arr.length;
54+
int[] result = new int[n];
55+
Stack<Integer> indexStack = new Stack<>();
56+
for (int i = 0; i < n; i++) {
57+
while (!indexStack.isEmpty() && arr[i] <= arr[indexStack.peek()]) {
58+
indexStack.pop();
59+
}
60+
result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()];
61+
indexStack.push(i);
62+
}
63+
return result;
64+
}
65+
66+
public static void main(String[] args) {
67+
int[] sampleArray = {4, 5, 2, 10, 8};
68+
System.out.println("Nearest Greater to Right: " + Arrays.toString(nearestGreaterToRight(sampleArray)));
69+
System.out.println("Nearest Greater to Left: " + Arrays.toString(nearestGreaterToLeft(sampleArray)));
70+
System.out.println("Nearest Smaller to Right: " + Arrays.toString(nearestSmallerToRight(sampleArray)));
71+
System.out.println("Nearest Smaller to Left: " + Arrays.toString(nearestSmallerToLeft(sampleArray)));
72+
}
73+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.thealgorithms.datastructures.stacks;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
5+
6+
public class NearestElementTest {
7+
8+
@Test
9+
public void testNearestGreaterToRight() {
10+
int[] arr = {4, 5, 2, 10, 8};
11+
int[] expected = {5, 10, 10, -1, -1};
12+
assertArrayEquals(expected, NearestElement.nearestGreaterToRight(arr));
13+
}
14+
15+
@Test
16+
public void testNearestGreaterToLeft() {
17+
int[] arr = {4, 5, 2, 10, 8};
18+
int[] expected = {-1, -1, 5, -1, 10};
19+
assertArrayEquals(expected, NearestElement.nearestGreaterToLeft(arr));
20+
}
21+
22+
@Test
23+
public void testNearestSmallerToRight() {
24+
int[] arr = {4, 5, 2, 10, 8};
25+
int[] expected = {2, 2, -1, 8, -1};
26+
assertArrayEquals(expected, NearestElement.nearestSmallerToRight(arr));
27+
}
28+
29+
@Test
30+
public void testNearestSmallerToLeft() {
31+
int[] arr = {4, 5, 2, 10, 8};
32+
int[] expected = {-1, 4, -1, 2, 2};
33+
assertArrayEquals(expected, NearestElement.nearestSmallerToLeft(arr));
34+
}
35+
}

0 commit comments

Comments
 (0)