Skip to content
Closed
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
73 changes: 73 additions & 0 deletions src/main/java/com/thealgorithms/stacks/NearestElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.thealgorithms.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<Integer> 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<Integer> 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<Integer> 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<Integer> 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)));
}
}
56 changes: 56 additions & 0 deletions src/test/java/com/thealgorithms/stacks/NearestElementTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.thealgorithms.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();
}
}
Loading