Next Greater Element I
You are given two arrays of distinct integers, nums1 and nums2, where nums1 is a subset of nums2. For each number in nums1, find the first number to its right in nums2 that is greater than it. If no such number exists, return -1 for that position.
This is a premium problem.
Unlock Next Greater Element I with both full courses, DSA and System Design, and every other premium problem.
Problem
The next greater element of some element x in an array is the first greater element that is to the right of x in the same array. You are given two distinct 0-indexed integer arrays nums1 and nums2. For each element in nums1, find its next greater element in nums2.
Input
Two integer arrays `nums1` and `nums2` of distinct integers. `nums1` is a subset of `nums2`.
Output
An array `answer` where `answer[i]` is the next greater element of `nums1[i]` in `nums2`, or -1 if none exists.
Examples
Input: nums1 = [4, 1, 2], nums2 = [1, 3, 4, 2]
Output: [-1, 3, -1]
4 appears at index 2 in nums2; nothing to its right is greater → -1. 1 appears at index 0; the first number to its right greater than 1 is 3 → 3. 2 is at index 3 (rightmost); nothing to its right → -1.
Input: nums1 = [2, 4], nums2 = [1, 2, 3, 4]
Output: [3, -1]
Next greater than 2 in nums2 is 3. 4 is the last element, no greater element exists.
The brute-force approach
For each element in nums1, find its position in nums2, then scan rightward through nums2 to find the first element larger than it.
answer = []
for x in nums1:
idx = nums2.index(x) # find x in nums2
found = -1
for j in range(idx + 1, len(nums2)): # scan right
if nums2[j] > x:
found = nums2[j]
break
answer.append(found)
return answerFor each of the m elements in nums1 you may scan up to n elements of nums2. O(m × n) total. The next greater element for every value in nums2 can be computed in a single O(n) pass using a monotonic stack. Then answering queries for nums1 is just a hash map lookup. O(1) per element.
Spotting the pattern
This is a Stack problem. The key question to ask yourself:
How do I find the next greater element for every value in nums2 without scanning right from each position?
Answering that is where it clicks, and it's exactly what the guided walkthrough below builds with you: the pattern reasoning, a progressive hint ladder that never spoils the answer, a row-by-row dry run, the optimized solution, and an in-browser editor to run your code against real test cases.
Unlock the full guided solution
The Stack walkthrough for Next Greater Element I: the progressive hint ladder, a row-by-row dry run, the optimized code, and an in-browser runner, plus both full courses, DSA and System Design, and every other premium problem.
Already finished Big-O and Hash Maps free? This picks up right where the path leads: same format, harder patterns, the ones that show up in every FAANG-style screen.
What you get
- ✓The full DSA course: concept lessons, drills, graded tests, and cold-read capstones for every pattern
- ✓The full System Design course: 20 building-block units for the design round, included at no extra cost
- ✓Stop going blank on algorithm problems. The hint ladder walks you to the answer without spoiling it.
- ✓Actually internalize the trace: dry runs walk state row by row so it clicks before you write a line
- ✓Know when to reach for each pattern. Every problem names the trigger so you spot it next time.
- ✓Write and run JS or Python in the browser with real test cases on every problem
- ✓Access every new problem, unit, and pattern guide as they ship weekly
“This app finally made it click. I tried NeetCode, CTCI, and YouTube for years and still couldn't solve Two Sum. The beginner mental models for the patterns are genuinely so helpful. THANK YOU.”
Full access
$49.99 / year
or $9.99 / month
Now includes a C++ runner alongside JavaScript and Python.
LeetCode Premium is $159/year and doesn't teach you anything.
Less than one Udemy course, with guided problems instead of passive lectures.
One extra month of job searching costs more than a full year here.
- ✓Both courses, every unit: DSA + System Design
- ✓All 150 problems across 33 patterns, plus every new one
- ✓New problems and units weekly
- ✓Cancel anytime
Cancel anytime. Not useful within 7 days? Email for a full refund. Secured by Stripe.