Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.thealgorithms.datastructures.stacks;

import java.util.Stack;

/**
* The {@code NearestElement} class provides static utility methods to find the
* nearest greater or smaller elements to the left or right of each element in
* an integer array using stack-based algorithms.
*
* <p>
* Each method runs in O(n) time complexity by maintaining a monotonic stack:
* <ul>
* <li>{@code nearestGreaterToRight}: Finds the nearest greater element to the
* right of each element.</li>
* <li>{@code nearestGreaterToLeft}: Finds the nearest greater element to the
* left of each element.</li>
* <li>{@code nearestSmallerToRight}: Finds the nearest smaller element to the
* right of each element.</li>
* <li>{@code nearestSmallerToLeft}: Finds the nearest smaller element to the
* left of each element.</li>
* </ul>
*/
public final class NearestElement {

private NearestElement() {
throw new UnsupportedOperationException("Utility class");
}

public static int[] nearestGreaterToRight(int[] arr) {
int n = arr.length;
int[] result = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && stack.peek() <= arr[i]) {
stack.pop();
}
result[i] = stack.isEmpty() ? -1 : stack.peek();
stack.push(arr[i]);
}
return result;
}

public static int[] nearestGreaterToLeft(int[] arr) {
int n = arr.length;
int[] result = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && stack.peek() <= arr[i]) {
stack.pop();
}
result[i] = stack.isEmpty() ? -1 : stack.peek();
stack.push(arr[i]);
}
return result;
}

public static int[] nearestSmallerToRight(int[] arr) {
int n = arr.length;
int[] result = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && stack.peek() >= arr[i]) {
stack.pop();
}
result[i] = stack.isEmpty() ? -1 : stack.peek();
stack.push(arr[i]);
}
return result;
}

public static int[] nearestSmallerToLeft(int[] arr) {
int n = arr.length;
int[] result = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && stack.peek() >= arr[i]) {
stack.pop();
}
result[i] = stack.isEmpty() ? -1 : stack.peek();
stack.push(arr[i]);
}
return result;
}
}
59 changes: 59 additions & 0 deletions src/test/java/com/thealgorithms/stacks/NearestElementTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.thealgorithms.stacks;

import java.lang.reflect.Constructor;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;

import com.thealgorithms.datastructures.stacks.NearestElement;


class NearestElementTest {

@Test
void testNearestGreaterToRight() {
int[] arr = {4, 5, 2, 10, 8};
int[] result = NearestElement.nearestGreaterToRight(arr);
System.out.println("nearestGreaterToRight: " + Arrays.toString(result));
int[] expected = {5, 10, 10, -1, -1};
assertArrayEquals(expected, result);
}

@Test
void testNearestGreaterToLeft() {
int[] arr = {4, 5, 2, 10, 8};
int[] result = NearestElement.nearestGreaterToLeft(arr);
System.out.println("nearestGreaterToLeft: " + Arrays.toString(result));
int[] expected = {-1, -1, 5, -1, 10};
assertArrayEquals(expected, result);
}

@Test
void testNearestSmallerToRight() {
int[] arr = {4, 5, 2, 10, 8};
int[] result = NearestElement.nearestSmallerToRight(arr);
System.out.println("nearestSmallerToRight: " + Arrays.toString(result));
int[] expected = {2, 2, -1, 8, -1};
assertArrayEquals(expected, result);
}

@Test
void testNearestSmallerToLeft() {
int[] arr = {4, 5, 2, 10, 8};
int[] result = NearestElement.nearestSmallerToLeft(arr);
System.out.println("nearestSmallerToLeft: " + Arrays.toString(result));
int[] expected = {-1, 4, -1, 2, 2};
assertArrayEquals(expected, result);
}

@Test
void testPrivateConstructor() throws Exception {
Constructor<NearestElement> constructor = NearestElement.class.getDeclaredConstructor();
constructor.setAccessible(true);
try {
constructor.newInstance();
} catch (Exception ignored) {
}
}
}
Loading