Next Greater Element I - Problem
Next Greater Element I is a classic stack-based problem that teaches you how to efficiently find relationships between array elements.

Given two arrays nums1 and nums2, where nums1 is a subset of nums2, your task is to find the next greater element for each element in nums1.

The next greater element of an element x is the first element to the right of x that is greater than x. If no such element exists, return -1.

Example: If nums2 = [1,3,4,2] and we're looking for the next greater element of 3, the answer is 4 because 4 is the first element to the right of 3 that is greater than 3.

Goal: Return an array where each position contains the next greater element for the corresponding element in nums1.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums1 = [4,1,2], nums2 = [1,3,4,2]
โ€บ Output: [-1,3,-1]
๐Ÿ’ก Note: For 4: no element to the right is greater, so -1. For 1: first greater element to the right is 3. For 2: no element to the right is greater, so -1.
example_2.py โ€” All Found
$ Input: nums1 = [2,4], nums2 = [1,2,3,4]
โ€บ Output: [3,-1]
๐Ÿ’ก Note: For 2: first greater element to the right is 3. For 4: no element to the right is greater, so -1.
example_3.py โ€” Single Element
$ Input: nums1 = [1], nums2 = [1,2]
โ€บ Output: [2]
๐Ÿ’ก Note: For 1: first greater element to the right is 2.

Visualization

Tap to expand
Monotonic Stack VisualizationInput: nums2 = [1, 3, 4, 2]1342Stack OperationsStep 1: Process 1Stack: [] โ†’ [1]Step 2: Process 33 > 1, so map[1] = 3Stack: [1] โ†’ [3]Step 3: Process 44 > 3, so map[3] = 4Stack: [3] โ†’ [4]Step 4: Process 22 < 4, so just pushStack: [4] โ†’ [4,2]Next Greater Map1 โ†’ 3 โœ“3 โ†’ 4 โœ“4 โ†’ -1 (no next greater)2 โ†’ -1 (no next greater)Query Phasenums1 = [4, 1, 2]result = [map[4], map[1], map[2]]result = [-1, 3, -1]๐Ÿ’ก Key Insight: Stack maintains decreasing order - when we find larger element, it's the answer for all smaller elements in stack!
Understanding the Visualization
1
Initialize Stack
Start with an empty stack and empty result map
2
Process Each Element
For each element in nums2, pop smaller elements from stack and map them to current element
3
Add Current Element
Push current element onto stack
4
Handle Remaining
Elements left in stack have no next greater element (-1)
5
Query Results
Look up each element from nums1 in our result map
Key Takeaway
๐ŸŽฏ Key Insight: The monotonic stack approach transforms an O(nยฒ) problem into O(n) by maintaining elements in decreasing order and efficiently mapping each element to its next greater neighbor.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n*m)

For each of n elements in nums1, we potentially scan all m elements in nums2

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables, no additional data structures

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums1.length โ‰ค nums2.length โ‰ค 1000
  • 0 โ‰ค nums1[i], nums2[i] โ‰ค 104
  • All integers in nums1 and nums2 are unique
  • All the integers of nums1 also appear in nums2
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 22
85.5K Views
High Frequency
~15 min Avg. Time
2.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen