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