easy Problem 4 Next Greater Element
Problem Statement
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 the array.
Elements for which no greater element exist, consider the next greater element as -1.
Examples
Example 1:
Input: [4, 5, 2, 25]
Output: [5, 25, 25, -1]
Example 1:
Input: [13, 7, 6, 12]
Output: [-1, 12, 12, -1]
Example 1:
Input: [1, 2, 3, 4, 5]
Output: [2, 3, 4, 5, -1]
Constraints:
- 1 <= arr.length <= 104
- -109 <= arr[i] <= 109
Try it yourself
Try solving this question here:
✅ Solution Next Greater Element
Problem Statement
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 the array.
Elements for which no greater element exist, consider the next greater element as -1.
Examples
Example 1:
Input: [4, 5, 2, 25]
Output: [5, 25, 25, -1]
Explanation: The NGE for 4 is 5, 5 is 25, 2 is 25, and there is no NGE for 25.
Example 1:
Input: [13, 7, 6, 12]
Output: [-1, 12, 12, -1]
Example 1:
Input: [1, 2, 3, 4, 5]
Output: [2, 3, 4, 5, -1]
Constraints:
- 1 <= arr.length <= 104
- -109 <= arr[i] <= 109
Solution:
A simple algorithm is to run two loops: the outer loop picks all elements one by one, and the inner loop looks for the first greater element for the element picked by the outer loop. However, this algorithm has a time complexity of
We can use a more optimized approach using Stack data structure. The algorithm will leverage the nature of the stack data structure, where the most recently added (pushed) elements are the first ones to be removed (popped). Starting from the end of the array, the algorithm always maintains elements in the stack that are larger than the current element. This way, it ensures that it has a candidate for the "next larger element". If there is no larger element, it assigns -1 to that position. It handles each element of the array only once, making it an efficient solution.
Detailed Step-by-Step Walkthrough
-
The function receives an array
arr. -
Initialize an empty stack
sand an output arrayresof size equal to the input array, with all elements initialized to -1.reswill store the result, i.e., the next larger element for each position in the array. -
Start a loop that goes from the last index of the array to the first (0 index).
-
In each iteration, while there are elements in the stack and the top element of the stack is less than or equal to the current element in the array, remove elements from the stack. This step ensures that we retain only the elements in the stack that are larger than the current element.
-
After the popping process, if there is still an element left in the stack, it is the next larger element for the current array element. So, assign the top element of the stack to the corresponding position in the
resarray. -
Now, push the current array element into the stack. This action considers the current element as a possible "next larger element" for the upcoming elements in the remaining iterations.
-
Repeat steps 4-6 for all the elements of the array.
-
At the end of the loop,
reswill contain the next larger element for each position in the array. Return this arrayres.
Algorithm Walkthrough
Let's consider the input and observe how above algorithm works.
-
Initialize Data Structures:
- Input Array:
[13, 7, 6, 12] - Result Array:
[0, 0, 0, 0](Initially set to zeros) - Stack: Empty (Will store elements during iteration)
- Input Array:
-
Processing Each Element (Reverse Order):
- The algorithm processes the array from right to left.
-
Last Element (Value 12):
- Stack is empty, indicating no greater element for 12.
- Result Array:
[0, 0, 0, -1](Updates the last position to -1) - Push element 12 onto the stack.
-
Third Element (Value 6):
- Stack's top element is 12, which is greater than 6.
- Result Array:
[0, 0, 12, -1](Updates the value at the third position to 12) - Push element 6 onto the stack.
-
Second Element (Value 7):
- Stack's top element is 6, which is less than 7, so it's popped.
- Next, the stack's top element is 12, which is greater than 7.
- Result Array:
[0, 12, 12, -1](Updates the value at the second position to 12) - Push element 7 onto the stack.
-
First Element (Value 13):
- Stack's top element is 7, which is less than 13, so it's popped.
- Next, stack's top element is 12, which is also less than 13, so it's popped.
- Stack is now empty, indicating no greater element for 13.
- Result Array:
[-1, 12, 12, -1](Updates the first position to -1) - Push element 13 onto the stack.
Here is the visual representation of the algorithm:
Code
Here is the code for this algorithm:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
public class Solution {
// Method to find the Next Greater Element (NGE) for each element in the list
List<Integer> nextLargerElement(List<Integer> arr) {
int n = arr.size();
Stack<Integer> s = new Stack<>();
Integer[] res = new Integer[n]; // Initialize an array to store results
// Iterate through the list in reverse order
for (int i = n - 1; i >= 0; i--) {
// Pop elements from the stack while they are smaller than or equal to the current element
while (!s.empty() && s.peek() <= arr.get(i)) {
s.pop();
}
// If the stack is empty, there is no greater element on the right, so set it to -1
// Otherwise, set it to the top element of the stack
res[i] = s.empty() ? -1 : s.peek();
// Push the current element onto the stack
s.push(arr.get(i));
}
// Convert the array to a list and return
return Arrays.asList(res);
}
public static void main(String[] args) {
List<Integer> arr1 = Arrays.asList(11, 13, 21, 3);
List<Integer> arr2 = Arrays.asList(4, 5, 2, 25);
List<Integer> arr3 = Arrays.asList(13, 7, 6, 12);
Solution nge = new Solution();
// Find and print the Next Greater Element (NGE) for each list
System.out.println(nge.nextLargerElement(arr1)); // Output: [13, 21, -1, -1]
System.out.println(nge.nextLargerElement(arr2)); // Output: [5, 25, 25, -1]
System.out.println(nge.nextLargerElement(arr3)); // Output: [-1, 12, 12, -1]
}
}
Complexity Analysis
Time Complexity
-
Single pass (reverse iteration): The algorithm iterates through the input list
arrin reverse order. Since each element is processed exactly once, this takestime, where Nis the number of elements in the list. -
Stack operations: For each element in the list, the algorithm performs push and pop operations on the stack. Each element is pushed onto the stack once, and it is popped from the stack at most once. Therefore, the total time complexity for stack operations is also
.
Overall time complexity:
Space Complexity
-
Stack space: The stack stores elements from the input list. In the worst case, if the input list is strictly decreasing, all elements will be pushed onto the stack, requiring
space. -
Result list: The result list stores the Next Greater Element (NGE) for each element in the input list, so it requires
space.
Overall space complexity:
🤖 Don't fully get this? Learn it with Claude
Stuck on Problem 4 Next Greater Element? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Problem 4 Next Greater Element** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Problem 4 Next Greater Element** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Problem 4 Next Greater Element**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Problem 4 Next Greater Element**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.