Here we will perform an algorithm which will implement the logic of "next greater element of the element in an array"
Description of algorithm:
var array = [5,3,2,10,6,8,1,4,12,7,4]
Output:
2 - 10
3 - 10
5 - 10
6 - 8
1 - 4
4 - 12
8 - 12
10 - 12
4 - No
7 - No
12 - No
Algorithm
1. Push the element in stack if
a. stack is empty
b. element is less than or equal to top element of the stack
2. If element is greater than top element, then pop elements of the stack till the element is less than or equal to the top element (or the stack is empty)