Skip to content

Commit 2b6e096

Browse files
authored
Create NearestElement.java
1 parent 9484c7e commit 2b6e096

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package datastructures.stacks;
2+
import java.util.Arrays;
3+
import java.util.Stack;
4+
5+
/**
6+
* NearestElement four classic stack based algorithms:
7+
*Implemented methods:
8+
* nearestGreaterToRight(int[] arr) - for each index i, first element to the right arr[i] or -1
9+
* nearestGreaterToLeft(int[] arr) - for each index i, first element to the left arr[i] or -1
10+
* nearestSmallerToRight(int[] arr) - for each index i, first element to the right arr[i] or -1
11+
* nearestSmallerToLeft(int[] arr) - for each index i, first element to the left arr[i] or -1
12+
* All methods run in O(n) time and O(n) extra space (stack + output).
13+
* Usage: Each method returns an int[] of the same length as input. If no nearest element satisfies the condition, -1 is used at that index.
14+
*/
15+
public class NearestElement {
16+
/**
17+
* For each element, finds the nearest greater element to its right.
18+
* Example: [4, 5, 2, 25] -> [5, 25, 25, -1]
19+
* Time: O(n), Space: O(n)
20+
*/
21+
public static int[] nearestGreaterToRight(int[] arr) {
22+
int n = arr.length;
23+
int[] res = new int[n];
24+
Arrays.fill(res, -1);
25+
Stack<Integer> st = new Stack<>(); // it holds indices
26+
27+
// traverse left to right, but we pop while current > stack top
28+
for (int i = 0; i < n; i++) {
29+
while (!st.isEmpty() && arr[i] > arr[st.peek()]) {
30+
res[st.pop()] = arr[i];
31+
}
32+
st.push(i);
33+
}
34+
return res;
35+
}
36+
37+
/**
38+
* For each element, finds the nearest greater element to its left.
39+
* Example: [4, 5, 2, 25] -> [-1, -1, 5, -1]
40+
* Time: O(n), Space: O(n)
41+
*/
42+
public static int[] nearestGreaterToLeft(int[] arr) {
43+
int n = arr.length;
44+
int[] res = new int[n];
45+
Arrays.fill(res, -1);
46+
Stack<Integer> st = new Stack<>();
47+
for (int i = 0; i < n; i++) {
48+
while (!st.isEmpty() && arr[st.peek()] <= arr[i]) {
49+
st.pop();
50+
}
51+
if (!st.isEmpty()) res[i] = arr[st.peek()];
52+
st.push(i);
53+
}
54+
return res;
55+
}
56+
57+
/**
58+
* For each element, finds the nearest smaller element to its right.
59+
* Example: [4, 5, 2, 25] -> [2, 2, -1, -1]
60+
* Time: O(n), Space: O(n)
61+
*/
62+
public static int[] nearestSmallerToRight(int[] arr) {
63+
int n = arr.length;
64+
int[] res = new int[n];
65+
Arrays.fill(res, -1);
66+
Stack<Integer> st = new Stack<>();
67+
for (int i = 0; i < n; i++) {
68+
while (!st.isEmpty() && arr[i] < arr[st.peek()]) {
69+
res[st.pop()] = arr[i];
70+
}
71+
st.push(i);
72+
}
73+
return res;
74+
}
75+
76+
/**
77+
* For each element, finds the nearest smaller element to its left.
78+
* Example: [4, 5, 2, 25] -> [-1, 4, -1, 2]
79+
* Time: O(n), Space: O(n)
80+
*/
81+
public static int[] nearestSmallerToLeft(int[] arr) {
82+
int n = arr.length;
83+
int[] res = new int[n];
84+
Arrays.fill(res, -1);
85+
Stack<Integer> st = new Stack<>(); // holds indices
86+
for (int i = 0; i < n; i++) {
87+
while (!st.isEmpty() && arr[st.peek()] >= arr[i]) {
88+
st.pop();
89+
}
90+
if (!st.isEmpty()) res[i] = arr[st.peek()];
91+
st.push(i);
92+
}
93+
return res;
94+
}
95+
96+
public static void main(String[] args) {
97+
int[] arr = {4, 5, 2, 25, 1, 7};
98+
System.out.println("Input: " + Arrays.toString(arr));
99+
System.out.println("Nearest Greater to Right: " + Arrays.toString(nearestGreaterToRight(arr)));
100+
System.out.println("Nearest Greater to Left: " + Arrays.toString(nearestGreaterToLeft(arr)));
101+
System.out.println("Nearest Smaller to Right: " + Arrays.toString(nearestSmallerToRight(arr)));
102+
System.out.println("Nearest Smaller to Left: " + Arrays.toString(nearestSmallerToLeft(arr)));
103+
}
104+
}

0 commit comments

Comments
 (0)