Skip to content

Commit 7603303

Browse files
authored
Update MonotonicIncreasingStack.java
1 parent c3bc74c commit 7603303

File tree

1 file changed

+67
-65
lines changed

1 file changed

+67
-65
lines changed

src/test/java/com/thealgorithms/stacks/MonotonicIncreasingStack.java

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,84 +10,86 @@
1010
* https://www.geeksforgeeks.org/dsa/introduction-to-monotonic-stack-2/
1111
*/
1212

13+
package com.thealgorithms.stacks;
14+
1315
import java.util.Stack;
1416

1517
public final class MonotonicIncreasingStack {
1618

17-
private MonotonicIncreasingStack() {
18-
throw new AssertionError("Cannot instantiate utility class");
19-
}
19+
private MonotonicIncreasingStack() {
20+
throw new AssertionError("Cannot instantiate utility class");
21+
}
2022

21-
/**
22-
* Finds the next greater element for each element in the given array.
23-
*
24-
* For each element, it returns the nearest greater element to its right.
25-
* If no such element exists, -1 is returned for that index.
26-
*
27-
* Time Complexity: O(n)
28-
* Space Complexity: O(n)
29-
*/
30-
public static int[] nextGreaterElement(int[] arr) {
31-
int n = arr.length;
32-
int[] result = new int[n];
33-
Stack<Integer> stack = new Stack<>();
23+
/**
24+
* Finds the next greater element for each element in the given array.
25+
* For each element, it returns the nearest greater element to its right.
26+
* If no such element exists, -1 is returned for that index.
27+
*
28+
* Time Complexity: O(n)
29+
* Space Complexity: O(n)
30+
*/
31+
public static int[] nextGreaterElement(int[] arr) {
32+
int n = arr.length;
33+
int[] result = new int[n];
34+
Stack<Integer> stack = new Stack<>();
3435

35-
for (int i = n - 1; i >= 0; i--) {
36-
while (!stack.isEmpty() && arr[i] >= arr[stack.peek()]) {
37-
stack.pop();
38-
}
39-
result[i] = stack.isEmpty() ? -1 : arr[stack.peek()];
40-
stack.push(i);
41-
}
42-
return result;
36+
for (int i = n - 1; i >= 0; i--) {
37+
while (!stack.isEmpty() && arr[i] >= arr[stack.peek()]) {
38+
stack.pop();
39+
}
40+
result[i] = stack.isEmpty() ? -1 : arr[stack.peek()];
41+
stack.push(i);
4342
}
43+
return result;
44+
}
4445

45-
/**
46-
* Finds the next smaller element for each element in the given array.
47-
*
48-
* For each element, it returns the nearest smaller element to its right.
49-
* If no such element exists, -1 is returned for that index.
50-
*
51-
* Time Complexity: O(n)
52-
* Space Complexity: O(n)
53-
*/
54-
public static int[] nextSmallerElement(int[] arr) {
55-
int n = arr.length;
56-
int[] result = new int[n];
57-
Stack<Integer> stack = new Stack<>();
46+
/**
47+
* Finds the next smaller element for each element in the given array.
48+
* For each element, it returns the nearest smaller element to its right.
49+
* If no such element exists, -1 is returned for that index.
50+
*
51+
* Time Complexity: O(n)
52+
* Space Complexity: O(n)
53+
*/
54+
public static int[] nextSmallerElement(int[] arr) {
55+
int n = arr.length;
56+
int[] result = new int[n];
57+
Stack<Integer> stack = new Stack<>();
5858

59-
for (int i = n - 1; i >= 0; i--) {
60-
while (!stack.isEmpty() && arr[i] <= arr[stack.peek()]) {
61-
stack.pop();
62-
}
63-
result[i] = stack.isEmpty() ? -1 : arr[stack.peek()];
64-
stack.push(i);
65-
}
66-
return result;
59+
for (int i = n - 1; i >= 0; i--) {
60+
while (!stack.isEmpty() && arr[i] <= arr[stack.peek()]) {
61+
stack.pop();
62+
}
63+
result[i] = stack.isEmpty() ? -1 : arr[stack.peek()];
64+
stack.push(i);
6765
}
66+
return result;
67+
}
6868

69-
// Test class included in the same file
70-
public static void main(String[] args) {
71-
testScenario(new int[]{2, 5, 1, 3, 4});
72-
testScenario(new int[]{1, 2, 3, 4, 5});
73-
testScenario(new int[]{5, 4, 3, 2, 1});
74-
}
69+
// Test class included in the same file
70+
public static void main(String[] args) {
71+
testScenario(new int[]{2, 5, 1, 3, 4});
72+
testScenario(new int[]{1, 2, 3, 4, 5});
73+
testScenario(new int[]{5, 4, 3, 2, 1});
74+
}
7575

76-
private static void testScenario(int[] arr) {
77-
int[] nextGreater = nextGreaterElement(arr);
78-
int[] nextSmaller = nextSmallerElement(arr);
76+
private static void testScenario(int[] arr) {
77+
int[] nextGreater = nextGreaterElement(arr);
78+
int[] nextSmaller = nextSmallerElement(arr);
7979

80-
System.out.print("Array: ");
81-
printArray(arr);
82-
System.out.print("Next Greater: ");
83-
printArray(nextGreater);
84-
System.out.print("Next Smaller: ");
85-
printArray(nextSmaller);
86-
System.out.println("-----------------------------");
87-
}
80+
System.out.print("Array: ");
81+
printArray(arr);
82+
System.out.print("Next Greater: ");
83+
printArray(nextGreater);
84+
System.out.print("Next Smaller: ");
85+
printArray(nextSmaller);
86+
System.out.println("-----------------------------");
87+
}
8888

89-
private static void printArray(int[] arr) {
90-
for (int n : arr) System.out.print(n + " ");
91-
System.out.println();
89+
private static void printArray(int[] arr) {
90+
for (int n : arr) {
91+
System.out.print(n + " ");
9292
}
93+
System.out.println();
94+
}
9395
}

0 commit comments

Comments
 (0)