diff --git a/src/main/java/com/thealgorithms/stacks/NearestElement.java b/src/main/java/com/thealgorithms/stacks/NearestElement.java new file mode 100644 index 000000000000..78d8a2479d25 --- /dev/null +++ b/src/main/java/com/thealgorithms/stacks/NearestElement.java @@ -0,0 +1,73 @@ +package com.thealgorithms.datastructures.stacks; + +import java.util.Stack; +import java.util.Arrays; + +public final class NearestElement { + + private NearestElement() {} + + public static int[] nearestGreaterToRight(int[] arr) { + int n = arr.length; + int[] result = new int[n]; + Stack indexStack = new Stack<>(); + for (int i = n - 1; i >= 0; i--) { + while (!indexStack.isEmpty() && arr[i] >= arr[indexStack.peek()]) { + indexStack.pop(); + } + result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()]; + indexStack.push(i); + } + return result; + } + + public static int[] nearestGreaterToLeft(int[] arr) { + int n = arr.length; + int[] result = new int[n]; + Stack indexStack = new Stack<>(); + for (int i = 0; i < n; i++) { + while (!indexStack.isEmpty() && arr[i] >= arr[indexStack.peek()]) { + indexStack.pop(); + } + result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()]; + indexStack.push(i); + } + return result; + } + + public static int[] nearestSmallerToRight(int[] arr) { + int n = arr.length; + int[] result = new int[n]; + Stack indexStack = new Stack<>(); + for (int i = n - 1; i >= 0; i--) { + while (!indexStack.isEmpty() && arr[i] <= arr[indexStack.peek()]) { + indexStack.pop(); + } + result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()]; + indexStack.push(i); + } + return result; + } + + public static int[] nearestSmallerToLeft(int[] arr) { + int n = arr.length; + int[] result = new int[n]; + Stack indexStack = new Stack<>(); + for (int i = 0; i < n; i++) { + while (!indexStack.isEmpty() && arr[i] <= arr[indexStack.peek()]) { + indexStack.pop(); + } + result[i] = indexStack.isEmpty() ? -1 : arr[indexStack.peek()]; + indexStack.push(i); + } + return result; + } + + public static void main(String[] args) { + int[] sampleArray = {4, 5, 2, 10, 8}; + System.out.println("Nearest Greater to Right: " + Arrays.toString(nearestGreaterToRight(sampleArray))); + System.out.println("Nearest Greater to Left: " + Arrays.toString(nearestGreaterToLeft(sampleArray))); + System.out.println("Nearest Smaller to Right: " + Arrays.toString(nearestSmallerToRight(sampleArray))); + System.out.println("Nearest Smaller to Left: " + Arrays.toString(nearestSmallerToLeft(sampleArray))); + } +} diff --git a/src/test/java/com/thealgorithms/stacks/NearestElementTest.java b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java new file mode 100644 index 000000000000..e77e3f0ca82b --- /dev/null +++ b/src/test/java/com/thealgorithms/stacks/NearestElementTest.java @@ -0,0 +1,58 @@ +package com.thealgorithms.datastructures.stacks; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +public class NearestElementTest { + + @Test + public void testNearestGreaterToRight() { + int[] arr = {4, 5, 2, 10, 8}; + int[] expected = {5, 10, 10, -1, -1}; + assertArrayEquals(expected, NearestElement.nearestGreaterToRight(arr)); + } + + @Test + public void testNearestGreaterToLeft() { + int[] arr = {4, 5, 2, 10, 8}; + int[] expected = {-1, -1, 5, -1, 10}; + assertArrayEquals(expected, NearestElement.nearestGreaterToLeft(arr)); + } + + @Test + public void testNearestSmallerToRight() { + int[] arr = {4, 5, 2, 10, 8}; + int[] expected = {2, 2, -1, 8, -1}; + assertArrayEquals(expected, NearestElement.nearestSmallerToRight(arr)); + } + + @Test + public void testNearestSmallerToLeft() { + int[] arr = {4, 5, 2, 10, 8}; + int[] expected = {-1, 4, -1, 2, 2}; + assertArrayEquals(expected, NearestElement.nearestSmallerToLeft(arr)); + } + + @Test + void testEmptyArray() { + int[] arr = {}; + assertArrayEquals(new int[]{}, NearestElement.nearestGreaterToRight(arr)); + assertArrayEquals(new int[]{}, NearestElement.nearestGreaterToLeft(arr)); +} + + @Test + void testAllEqualElements() { + int[] arr = {5, 5, 5, 5}; + assertArrayEquals(new int[]{-1, -1, -1, -1}, NearestElement.nearestGreaterToRight(arr)); + assertArrayEquals(new int[]{-1, -1, -1, -1}, NearestElement.nearestGreaterToLeft(arr)); +} + + @Test + void testPrivateConstructor() throws Exception { + var constructor = NearestElement.class.getDeclaredConstructor(); + constructor.setAccessible(true); + constructor.newInstance(); +} + + +}