Given an unsorted integer array, print all greater elements than all elements present to their right.

For example, consider the array [10, 4, 6, 3, 5]. The elements that are greater than all elements to their right are 10, 6, and 5.

Practice this problem

 
A naive solution would be to use two loops. For each element, check if any greater element exists to their right or not. If all elements to their right are less than it, print the element. The time complexity of this solution is O(n2), where n is the size of the input.

 
A better solution is to use a stack. For each element, pop all the elements present in the stack that are less than it and then push it into the stack. Finally, the stack is left with the elements which are greater than all elements present to their right. Following is the C++, Java, and Python program that demonstrates it:

C++


Download  Run Code

Output:

5 6 10

Java


Download  Run Code

Output:

5 6 10

Python


Download  Run Code

Output:

5 6 10

The time complexity of the above solution is O(n) and requires O(n) extra space.

 
We can easily solve this problem in linear time using constant space. The idea is to traverse the array from right to left and maintain a variable that stores the maximum element encountered so far. So if the current element is greater than the maximum so far, print the current element and update the maximum so far. This approach is demonstrated below in C, Java, and Python:

C


Download  Run Code

Output:

5 6 10

Java


Download  Run Code

Output:

5 6 10

Python


Download  Run Code

Output:

5 6 10