Skip to content

Added Next-Grater , Next-Smaller Elements using Stack #2858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.thealgorithms.datastructures.stacks;
import java.util.Arrays;
import java.util.Stack;
/*
Given an array "input" you need to print the first grater element for each element.
For a given element x of an array, the Next Grater element of that element is the
first grater element to the right side of it. If no such element is present print -1.

Example
input = { 2, 7, 3, 5, 4, 6, 8 };
At i = 0
Next Grater element between (1 to n) is 7
At i = 1
Next Grater element between (2 to n) is 8
At i = 2
Next Grater element between (3 to n) is 5
At i = 3
Next Grater element between (4 to n) is 6
At i = 4
Next Grater element between (5 to n) is 6
At i = 5
Next Grater element between (6 to n) is 8
At i = 6
Next Grater element between (6 to n) is -1

result : [7, 8, 5, 6, 6, 8, -1]

1. If the stack is empty Push an element in the stack.
2. If the stack is not empty:
a. compare the top element of the stack with next.
b. If next is greater than the top element, Pop element from the stack.
next is the next greater element for the popped element.
c. Keep popping from the stack while the popped element is smaller
than next. next becomes the next greater element for all such
popped elements.
d. Finally, push the next in the stack.

3. If elements are left in stack after completing while loop then their Next Grater element is -1.
*/


public class NextGraterElement {

public static int[] findNextGreaterElements(int[] array) {

if (array == null) {
return array;
}

int[] result = new int[array.length];
Stack<Integer> stack = new Stack<>();

for (int i = 0; i < array.length; i++) {
while (!stack.isEmpty() && array[stack.peek()] < array[i]) {
result[stack.pop()] = array[i];
}
stack.push(i);
}

return result;
}

public static void main(String[] args)
{
int[] input = { 2, 7, 3, 5, 4, 6, 8 };
int[] result = findNextGreaterElements(input);
System.out.println(Arrays.toString(result));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.thealgorithms.datastructures.stacks;
import java.util.Arrays;
import java.util.Stack;


/*
Given an array "input" you need to print the first smaller element for each element to the left side of an array.
For a given element x of an array, the Next Smaller element of that element is the
first smaller element to the left side of it. If no such element is present print -1.

Example
input = { 2, 7, 3, 5, 4, 6, 8 };
At i = 0
No elements to left of it : -1
At i = 1
Next smaller element between (0 , 0) is 2
At i = 2
Next smaller element between (0 , 1) is 2
At i = 3
Next smaller element between (0 , 2) is 3
At i = 4
Next smaller element between (0 , 3) is 4
At i = 5
Next smaller element between (0 , 4) is 3
At i = 6
Next smaller element between (0 , 5) is 6

result : [-1, 2, 2, 3, 3, 4, 6]

1) Create a new empty stack st

2) Iterate over array "input" , where "i" goes from 0 to input.length -1.
a) We are looking for value just smaller than `input[i]`. So keep popping from "stack"
till elements in "stack.peek() >= input[i]" or stack becomes empty.
b) If the stack is non-empty, then the top element is our previous element. Else the previous element does not exist.
c) push input[i] in stack.
3) If elements are left then their answer is -1
*/

public class NextSmallerElement {
public static int[] findNextSmallerElements(int[] array)
{
// base case
if (array == null) {
return array;
}
Stack<Integer> stack = new Stack<>();
int[] result = new int[array.length];
Arrays.fill(result, -1);

for (int i = 0; i < array.length; i++) {
while (!stack.empty() && stack.peek() >= array[i]) stack.pop();
if (stack.empty()) {
result[i] = -1;
} else {
result[i] = stack.peek();
}
stack.push(array[i]);
}
return result;
}

public static void main(String[] args)
{
int[] input = { 2, 7, 3, 5, 4, 6, 8 };
int[] result = findNextSmallerElements(input);
System.out.println(Arrays.toString(result));
}
}