Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in array. Elements for which no greater element exist, consider next greater element as -1. The next greater elements should be printed in same order as input array.
Input : arr = [4, 5, 2, 25}
Output : 5 25 25 -1
Input : arr = [4, 5, 2, 25, 10}
Output : 5 25 25 -1 -1
We have discussed a solution here that does not print same order. Here we traverse array from rightmost element.
In this approach we have started iterating from the last element(nth) to the first(1st) element
The benefit is that when we arrive at a certain index his next greater element will be already in stack and we can directly get this element while at the same index.
- After reaching a certain index we will pop the stack till we get the greater element on top from the current element and that element will be the answer for current element
- If stack gets empty while doing the pop operation then the answer would be -1
Then we will stored the answer in an array for the current index.
Below is the implementation of above approach:
11 -- 13 13 -- 21 21 -- -1 3 -- -1
Time Complexity: O(n)
Auxiliary Space: O(n) There is no extra space required if you want to print the next greater of each element in reverse order of input(means first, for the last element and then for second last and so on till the first element)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximize the number of indices such that element is greater than element to its left
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Longest Subarray with first element greater than or equal to Last element
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Replace every element with the least greater element on its right
- Next Greater Element
- Next Greater Frequency Element
- Previous greater element
- Find just strictly greater element from first array for each element in second array
- Smallest Greater Element on Right Side
- Count elements whose sum with K is greater than max element
- Smallest element greater than X not present in the array
- Count of subarrays whose maximum element is greater than k
- Longest subsequence with first and last element greater than all other elements
- Find closest greater value for every element in array
- Find the element before which all the elements are smaller than it, and after which all are greater
- Elements greater than the previous and next element in an Array
- Find the next greater element in a Circular Array
- Count of greater elements for each element in the Array
- Closest greater or same value on left side for every element in array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.