Challenge - 5 Problems
Next Greater Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Next Greater Element for a given array
What is the output of the following Java code that finds the next greater element for each element in the array using a stack?
DSA Java
import java.util.*; public class Main { public static void main(String[] args) { int[] arr = {4, 5, 2, 25}; int[] result = nextGreaterElement(arr); System.out.println(Arrays.toString(result)); } public static int[] nextGreaterElement(int[] nums) { int n = nums.length; int[] res = new int[n]; Stack<Integer> stack = new Stack<>(); for (int i = n - 1; i >= 0; i--) { while (!stack.isEmpty() && stack.peek() <= nums[i]) { stack.pop(); } res[i] = stack.isEmpty() ? -1 : stack.peek(); stack.push(nums[i]); } return res; } }
Attempts:
2 left
💡 Hint
Think about how the stack helps find the next greater element by storing candidates from right to left.
✗ Incorrect
The code iterates from right to left, using a stack to keep track of elements that could be the next greater element. For each element, it pops smaller or equal elements from the stack until it finds a greater one or the stack is empty. The result array stores the next greater element or -1 if none exists.
🧠 Conceptual
intermediateUnderstanding the role of stack in Next Greater Element
Why do we use a stack when solving the Next Greater Element problem?
Attempts:
2 left
💡 Hint
Think about how the stack helps remember elements while scanning the array.
✗ Incorrect
The stack stores elements for which the next greater element is not yet found. As we scan from right to left, we pop smaller elements and find the next greater element for the current element.
🔧 Debug
advancedIdentify the error in Next Greater Element implementation
What error will the following code produce when executed?
DSA Java
import java.util.*; public class Main { public static void main(String[] args) { int[] arr = {2, 7, 3, 5}; int[] result = nextGreaterElement(arr); System.out.println(Arrays.toString(result)); } public static int[] nextGreaterElement(int[] nums) { int n = nums.length; int[] res = new int[n]; Stack<Integer> stack = new Stack<>(); for (int i = 0; i < n; i++) { while (!stack.isEmpty() && stack.peek() <= nums[i]) { stack.pop(); } res[i] = stack.isEmpty() ? -1 : stack.peek(); stack.push(nums[i]); } return res; } }
Attempts:
2 left
💡 Hint
Check the direction of iteration and how the stack is used.
✗ Incorrect
The code iterates from left to right, which causes the stack to only contain elements smaller or equal to the current element, resulting in no next greater element found for any element, so all results are -1.
🚀 Application
advancedNext Greater Element for Circular Array
Given a circular array where the next greater element for the last element may be at the start, what is the output of the code below?
DSA Java
import java.util.*; public class Main { public static void main(String[] args) { int[] nums = {1, 2, 1}; int[] res = nextGreaterElements(nums); System.out.println(Arrays.toString(res)); } public static int[] nextGreaterElements(int[] nums) { int n = nums.length; int[] res = new int[n]; Arrays.fill(res, -1); Stack<Integer> stack = new Stack<>(); for (int i = 0; i < 2 * n; i++) { int num = nums[i % n]; while (!stack.isEmpty() && nums[stack.peek()] < num) { res[stack.pop()] = num; } if (i < n) stack.push(i); } return res; } }
Attempts:
2 left
💡 Hint
Remember the array is circular, so elements at the start can be next greater for elements at the end.
✗ Incorrect
The code simulates a circular array by iterating twice the length. For each element, it finds the next greater element considering wrap-around. For input [1,2,1], the next greater elements are [2, -1, 2].
❓ Predict Output
expertNext Greater Element with duplicates and negative numbers
What is the output of the following code that finds the next greater element for each element in the array containing duplicates and negative numbers?
DSA Java
import java.util.*; public class Main { public static void main(String[] args) { int[] arr = {2, -1, 2, 3, 2}; int[] result = nextGreaterElement(arr); System.out.println(Arrays.toString(result)); } public static int[] nextGreaterElement(int[] nums) { int n = nums.length; int[] res = new int[n]; Stack<Integer> stack = new Stack<>(); for (int i = n - 1; i >= 0; i--) { while (!stack.isEmpty() && stack.peek() <= nums[i]) { stack.pop(); } res[i] = stack.isEmpty() ? -1 : stack.peek(); stack.push(nums[i]); } return res; } }
Attempts:
2 left
💡 Hint
Duplicates and negative numbers do not affect the stack logic; focus on the next greater element for each position.
✗ Incorrect
The code correctly finds the next greater element for each element, even with duplicates and negatives. For example, for the first 2, the next greater is 3; for -1, next greater is 2; for the second 2, next greater is 3; for 3, no greater element; for last 2, no greater element.
