Introduction
The Next Greater Element I problem involves finding the next greater element for each number.
Given:
- nums1
- nums2
Rules:
- for every element in nums1
find the first greater element
to its right in nums2 - if no greater element exists:
return -1
This problem is one of the most important applications of:
Monotonic StackThis problem helps in understanding:
- stack traversal
- monotonic patterns
- next greater logic
- efficient searching
Example
Input:nums1 = [4,1,2]
nums2 = [1,3,4,2]
Output:
[-1,3,-1]
Explanation:
4 → no greater element
1 → next greater is 3
2 → no greater element
Constraints
1 <= nums1.length <= nums2.length <= 10000 <= nums1[i], nums2[i] <= 10^4
All integers are unique
Approach 1 : Brute Force Traversal
Explanation
The simplest way is:
- Find element position in nums2
- Traverse right side
- Find first greater element
This works but repeated scanning is inefficient.
Steps
- Traverse nums1.
- Locate element in nums2.
- Search right side.
- Store next greater element.
- Return result array.
Dry Run
nums1:[4,1,2]nums2:
[1,3,4,2]
For 4:
No greater element
For 1:
3 is greater
For 2: No greater element
Result: [-1,3,-1]
Brute Force Code
Complexity Analysis
Time Complexity: O(n * m)
Explanation:
Nested traversal occurs repeatedly.
Space Complexity: O(1)
Explanation:
Only output array is used.Approach 2 : Monotonic Stack
Explanation
The optimized solution uses:
Monotonic Decreasing Stack Idea:
- maintain decreasing stack
- whenever larger element appears:
- it becomes next greater element
Process:
- Traverse nums2
- Resolve smaller stack elements
- Store mappings
- Build result using hashmap
This avoids repeated searching.
Steps
- Create stack.
- Traverse nums2.
- Pop smaller elements.
- Store next greater mapping.
- Build answer using hashmap.
Dry Run
nums2:[1,3,4,2]
Stack: []
Push 1
Stack: [1]
Encounter 3 3 > 1
Mapping: 1 → 3
Push 3
Encounter 4 4 > 3
Mapping: 3 → 4
Final Mapping: 1 → 3
3 → 4
4 → -1
2 → -1
Monotonic Stack Code
Complexity Analysis
Time Complexity: O(n + m)
Explanation:
Each element is pushed and popped once.
Space Complexity: O(m)
Explanation:
Stack and hashmap are used.Edge Cases
- No greater element
- Strictly decreasing array
- Single element array
- Greater element immediately next
- Large arrays
Why This Problem is Important
Next Greater Element I helps in understanding:
- Monotonic stacks
- Stack-based optimization
- Efficient searching
- Next greater patterns
- Linear traversal optimization
It is one of the most important monotonic stack interview problems.
Real-World Applications
Next greater concepts are used in:
- Stock market analysis
- Temperature forecasting
- Signal processing
- Performance monitoring
- Sliding window systems
Common Mistakes
- Incorrect stack condition
- Forgetting remaining elements
- Wrong hashmap updates
- Using increasing stack instead
Interview Tips
Interviewers often expect:
- Monotonic stack explanation
- Linear optimization reasoning
- Stack pop condition understanding
Always explain:
- why decreasing stack is maintained
- how next greater elements are resolved
- why each element is processed once
Related Questions
- Valid Parentheses
- Min Stack
- Implement Stack using Queue
- Daily Temperatures
- Largest Rectangle in Histogram
Final Takeaway
The Next Greater Element I problem is a fundamental monotonic stack problem that teaches efficient next-greater searching and stack optimization techniques. Understanding this problem builds a strong foundation for advanced monotonic stack and linear traversal interview problems.