Skip to content

Commit 1f50c40

Browse files
authored
Add Next Greater and Next Smaller Elements using Stack (TheAlgorithms#2858)
1 parent bc6de37 commit 1f50c40

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.thealgorithms.datastructures.stacks;
2+
import java.util.Arrays;
3+
import java.util.Stack;
4+
/*
5+
Given an array "input" you need to print the first grater element for each element.
6+
For a given element x of an array, the Next Grater element of that element is the
7+
first grater element to the right side of it. If no such element is present print -1.
8+
9+
Example
10+
input = { 2, 7, 3, 5, 4, 6, 8 };
11+
At i = 0
12+
Next Grater element between (1 to n) is 7
13+
At i = 1
14+
Next Grater element between (2 to n) is 8
15+
At i = 2
16+
Next Grater element between (3 to n) is 5
17+
At i = 3
18+
Next Grater element between (4 to n) is 6
19+
At i = 4
20+
Next Grater element between (5 to n) is 6
21+
At i = 5
22+
Next Grater element between (6 to n) is 8
23+
At i = 6
24+
Next Grater element between (6 to n) is -1
25+
26+
result : [7, 8, 5, 6, 6, 8, -1]
27+
28+
1. If the stack is empty Push an element in the stack.
29+
2. If the stack is not empty:
30+
a. compare the top element of the stack with next.
31+
b. If next is greater than the top element, Pop element from the stack.
32+
next is the next greater element for the popped element.
33+
c. Keep popping from the stack while the popped element is smaller
34+
than next. next becomes the next greater element for all such
35+
popped elements.
36+
d. Finally, push the next in the stack.
37+
38+
3. If elements are left in stack after completing while loop then their Next Grater element is -1.
39+
*/
40+
41+
42+
public class NextGraterElement {
43+
44+
public static int[] findNextGreaterElements(int[] array) {
45+
46+
if (array == null) {
47+
return array;
48+
}
49+
50+
int[] result = new int[array.length];
51+
Stack<Integer> stack = new Stack<>();
52+
53+
for (int i = 0; i < array.length; i++) {
54+
while (!stack.isEmpty() && array[stack.peek()] < array[i]) {
55+
result[stack.pop()] = array[i];
56+
}
57+
stack.push(i);
58+
}
59+
60+
return result;
61+
}
62+
63+
public static void main(String[] args)
64+
{
65+
int[] input = { 2, 7, 3, 5, 4, 6, 8 };
66+
int[] result = findNextGreaterElements(input);
67+
System.out.println(Arrays.toString(result));
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.thealgorithms.datastructures.stacks;
2+
import java.util.Arrays;
3+
import java.util.Stack;
4+
5+
6+
/*
7+
Given an array "input" you need to print the first smaller element for each element to the left side of an array.
8+
For a given element x of an array, the Next Smaller element of that element is the
9+
first smaller element to the left side of it. If no such element is present print -1.
10+
11+
Example
12+
input = { 2, 7, 3, 5, 4, 6, 8 };
13+
At i = 0
14+
No elements to left of it : -1
15+
At i = 1
16+
Next smaller element between (0 , 0) is 2
17+
At i = 2
18+
Next smaller element between (0 , 1) is 2
19+
At i = 3
20+
Next smaller element between (0 , 2) is 3
21+
At i = 4
22+
Next smaller element between (0 , 3) is 4
23+
At i = 5
24+
Next smaller element between (0 , 4) is 3
25+
At i = 6
26+
Next smaller element between (0 , 5) is 6
27+
28+
result : [-1, 2, 2, 3, 3, 4, 6]
29+
30+
1) Create a new empty stack st
31+
32+
2) Iterate over array "input" , where "i" goes from 0 to input.length -1.
33+
a) We are looking for value just smaller than `input[i]`. So keep popping from "stack"
34+
till elements in "stack.peek() >= input[i]" or stack becomes empty.
35+
b) If the stack is non-empty, then the top element is our previous element. Else the previous element does not exist.
36+
c) push input[i] in stack.
37+
3) If elements are left then their answer is -1
38+
*/
39+
40+
public class NextSmallerElement {
41+
public static int[] findNextSmallerElements(int[] array)
42+
{
43+
// base case
44+
if (array == null) {
45+
return array;
46+
}
47+
Stack<Integer> stack = new Stack<>();
48+
int[] result = new int[array.length];
49+
Arrays.fill(result, -1);
50+
51+
for (int i = 0; i < array.length; i++) {
52+
while (!stack.empty() && stack.peek() >= array[i]) stack.pop();
53+
if (stack.empty()) {
54+
result[i] = -1;
55+
} else {
56+
result[i] = stack.peek();
57+
}
58+
stack.push(array[i]);
59+
}
60+
return result;
61+
}
62+
63+
public static void main(String[] args)
64+
{
65+
int[] input = { 2, 7, 3, 5, 4, 6, 8 };
66+
int[] result = findNextSmallerElements(input);
67+
System.out.println(Arrays.toString(result));
68+
}
69+
}

0 commit comments

Comments
 (0)