|
| 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 | +} |
0 commit comments