|
1 | | -package datastructures.stacks; |
| 1 | +package com.thealgorithms.datastructures.stacks; |
2 | 2 |
|
3 | | -import java.util.Arrays; |
4 | 3 | import java.util.Stack; |
5 | 4 |
|
6 | 5 | /** |
7 | 6 | * The {@code NearestElement} class provides static utility methods to find the nearest greater or smaller elements |
8 | 7 | * to the left or right of each element in an integer array using stack-based algorithms. |
9 | | - * |
| 8 | + * |
10 | 9 | * <p>Each method runs in O(n) time complexity by maintaining a monotonic stack: |
11 | 10 | * <ul> |
12 | 11 | * <li>{@code nearestGreaterToRight}: Finds the nearest greater element to the right of each element.</li> |
13 | 12 | * <li>{@code nearestGreaterToLeft}: Finds the nearest greater element to the left of each element.</li> |
14 | 13 | * <li>{@code nearestSmallerToRight}: Finds the nearest smaller element to the right of each element.</li> |
15 | 14 | * <li>{@code nearestSmallerToLeft}: Finds the nearest smaller element to the left of each element.</li> |
16 | 15 | * </ul> |
17 | | - * If no such element exists for a position, -1 is returned at that index. |
18 | 16 | */ |
19 | | -public class NearestElement { |
| 17 | +public final class NearestElement { |
| 18 | + |
| 19 | + private NearestElement() { |
| 20 | + throw new UnsupportedOperationException("Utility class"); |
| 21 | + } |
| 22 | + |
20 | 23 | public static int[] nearestGreaterToRight(int[] arr) { |
21 | 24 | int n = arr.length; |
22 | | - int[] res = new int[n]; |
23 | | - Stack<Integer> st = new Stack<>(); |
| 25 | + int[] result = new int[n]; |
| 26 | + Stack<Integer> stack = new Stack<>(); |
24 | 27 | 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 | + while (!stack.isEmpty() && stack.peek() <= arr[i]) { |
| 29 | + stack.pop(); |
| 30 | + } |
| 31 | + result[i] = stack.isEmpty() ? -1 : stack.peek(); |
| 32 | + stack.push(arr[i]); |
28 | 33 | } |
29 | | - return res; |
| 34 | + return result; |
30 | 35 | } |
31 | 36 |
|
32 | 37 | public static int[] nearestGreaterToLeft(int[] arr) { |
33 | 38 | int n = arr.length; |
34 | | - int[] res = new int[n]; |
35 | | - Stack<Integer> st = new Stack<>(); |
| 39 | + int[] result = new int[n]; |
| 40 | + Stack<Integer> stack = new Stack<>(); |
36 | 41 | 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]); |
| 42 | + while (!stack.isEmpty() && stack.peek() <= arr[i]) { |
| 43 | + stack.pop(); |
| 44 | + } |
| 45 | + result[i] = stack.isEmpty() ? -1 : stack.peek(); |
| 46 | + stack.push(arr[i]); |
40 | 47 | } |
41 | | - return res; |
| 48 | + return result; |
42 | 49 | } |
43 | 50 |
|
44 | 51 | public static int[] nearestSmallerToRight(int[] arr) { |
45 | 52 | int n = arr.length; |
46 | | - int[] res = new int[n]; |
47 | | - Stack<Integer> st = new Stack<>(); |
| 53 | + int[] result = new int[n]; |
| 54 | + Stack<Integer> stack = new Stack<>(); |
48 | 55 | 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]); |
| 56 | + while (!stack.isEmpty() && stack.peek() >= arr[i]) { |
| 57 | + stack.pop(); |
| 58 | + } |
| 59 | + result[i] = stack.isEmpty() ? -1 : stack.peek(); |
| 60 | + stack.push(arr[i]); |
52 | 61 | } |
53 | | - return res; |
| 62 | + return result; |
54 | 63 | } |
55 | 64 |
|
56 | 65 | public static int[] nearestSmallerToLeft(int[] arr) { |
57 | 66 | int n = arr.length; |
58 | | - int[] res = new int[n]; |
59 | | - Stack<Integer> st = new Stack<>(); |
| 67 | + int[] result = new int[n]; |
| 68 | + Stack<Integer> stack = new Stack<>(); |
60 | 69 | 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]); |
| 70 | + while (!stack.isEmpty() && stack.peek() >= arr[i]) { |
| 71 | + stack.pop(); |
| 72 | + } |
| 73 | + result[i] = stack.isEmpty() ? -1 : stack.peek(); |
| 74 | + stack.push(arr[i]); |
64 | 75 | } |
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))); |
| 76 | + return result; |
74 | 77 | } |
75 | 78 | } |
0 commit comments