Javatpoint Logo
Javatpoint Logo

Next Greater Element in Java

In an array, the next greater element is the element that is the nearest as well as the greater than the current element. Also, the next greater element should come after the current element. The task is to return the next greater element for every element present in an integer array. If the next greater element does not exist for an element of the array, then for that element, we will print the appropriate message. For simplicity, we will assume that every element of the input array is non-negative. Let's understand with the help of an example.

Input:

arr[] = {14, 16, 9, 90, 99, 2, 6, 19, 4}

Output with Explanation:

Element Next Greater Element (NGE) Reason
14 16 The element 16 is immediately next to 14. Also, 16 is greater than 14.
16 90 Elements that are greater than 16 are 90 and 99. However, we will pick 90 as the element 90 is the nearest to the element 16.
9 90 The element 90 is immediately next to 9. Also, 90 is greater than 9.
90 99 The element 99 is immediately next to 90. Also, 99 is greater than 90.
99 Not found There is no any element that comes after the element 99 and is greater than 99.
2 6 The element 6 is immediately next to 2. Also, 6 is greater than 2.
6 19 The element 19 is immediately next to 6. Also, 19 is greater than 6.
19 Not found There is no any element that comes after the element 19 and is greater than 19.
4 Not found There is no any element that comes after the element 4.

Note 1: For the last element, the next greater element will always be -1, as after the last element, we do not have any other element.

Note 2: For an input array that is sorted in non-increasing order, the value of the next greater element will always be -1. It is because all the next elements from the current element will always be less than or equal to the current element, as the elements are arranged in the non-decreasing order.

For example:

Input:

arr[] = {89, 60, 58, 45, 39, 36, 20}

Output with Explanation:

The next greater element of all the elements of the input array is -1.

For the number 89 there is no element that comes after 89 and is greater than 89.

For the number 60 there is no element that comes after 60 and is greater than 60.

A similar explanation can be given for the other elements too.

Approach

There are two approaches to solve the next greater element problem: one is the naïve approach, and the other is using a stack. Let's start with the naïve one.

Native Approach

The naïve approach is simple. One has to use two for-loops. The outer loop is for picking up the element, and the inner loop is for comparing the elements with the picked element (current element). The inner loop terminates immediately when a greater element (after comparing with the current element) is found. Observe the following program.

FileName: NGE.java

Output:

The input array is: 
14 16 9 90 99 2 6 19 4 

The next greater element for the element 14 is: 16
The next greater element for the element 16 is: 90
The next greater element for the element 9 is: 90
The next greater element for the element 90 is: 99
The next greater element for the element 99 does not exist. 
The next greater element for the element 2 is: 6
The next greater element for the element 6 is: 19
The next greater element for the element 19 does not exist. 
The next greater element for the element 4 does not exist.

Time Complexity: The above program is using two nested for-loops. Therefore, the time complexity of the above program is O(n2), where n is the total number of elements present in the input array.

Space Complexity: The program is not using any extra space. Therefore, the space complexity of the above program is O(1).

Optimized Approach: Using Stack

Algorithm

Step 1: In the stack, push the first element.

Step 2: Select the remaining elements one by one and repeat the mentioned sub-steps in the loop.

  1. Treat the current element as the next.
  2. If the stack is not vacant, compare the top element of the stack with the next.
  3. If the next is larger than the top element, then pop the element from the stack. next is the next larger element for the element that is popped.
  4. If the popped element is smaller than the next, keep popping elements from the stack. For all of such popped elements, the next becomes the next larger element.

Step 3: Eventually, the next in the stack is pushed.

Step 4: After the loop mentioned in step 2 is finished, keep popping from stack all the remaining elements, and display -1 for them as the next element.

FileName: NGE1.java

Output:

The input array is: 
14 16 9 90 99 2 6 19 4 

The next greater element for the element 14 is: 16
The next greater element for the element 9 is: 90
The next greater element for the element 16 is: 90
The next greater element for the element 90 is: 99
The next greater element for the element 2 is: 6
The next greater element for the element 6 is: 19
The next greater element for the element 4 does not exist.
The next greater element for the element 19 does not exist.
The next greater element for the element 99 does not exist.

Time Complexity: The time complexity of the above program is O(n), where n is the total number of elements present in the array.

Space Complexity: The space complexity of the above program is O(n), where n is the total number of elements present in the array.

We see that the order in which numbers are coming in the output is not the same in the input array. To maintain the order, have to traverse in the reverse order. Observe the following program.

FileName: NGE2.java

Output:

The input array is: 
14 16 9 90 99 2 6 19 4 

The next greater element for the element 14 is: 16
The next greater element for the element 16 is: 90
The next greater element for the element 9 is: 90
The next greater element for the element 90 is: 99
The next greater element for the element 99 does not exist.
The next greater element for the element 2 is: 6
The next greater element for the element 6 is: 19
The next greater element for the element 19 does not exist.
The next greater element for the element 4 does not exist.

Time Complexity: The time complexity of the above program is O(n), where n is the total number of elements present in the array.

Space Complexity: The space complexity of the above program is O(n), where n is the total number of elements present in the array.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA