Next Greater Element in JavaIn 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 nonnegative. Let's understand with the help of an example. Input: arr[] = {14, 16, 9, 90, 99, 2, 6, 19, 4} Output with Explanation:
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 nonincreasing 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 nondecreasing 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. ApproachThere 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 ApproachThe naïve approach is simple. One has to use two forloops. 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 forloops. Therefore, the time complexity of the above program is O(n^{2}), 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 StackAlgorithm Step 1: In the stack, push the first element. Step 2: Select the remaining elements one by one and repeat the mentioned substeps in the loop.
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.
Next TopicStar Numbers in Java
