Given an array, print the Next Greater Element for every element.
Next greater element for an element is the first greater element to the right of the array.
[4, 5, 2, 25] the answer is [5, 25, 25, None]
[13, 7, 6, 12] the answer is [None, 12, 12, None]
We can make the following observations from the problem:
1. NGE for the last element will always be None.
2. If the array is reversely sorted, all the NGEs are None.
3. We can start from the last element from the array and work our way towards the first element.
1. Start with the last element of the array.
2. Keep stack to maintain the next greater element.
3. For each element,
4. Keep popping from the stack until we find a greater element at the top of the stack.
5. If stack becomes empty, there is no NGE for the current element.
6. Push the current element to the stack.
The above approach can be implemented as follows: