Skip to content
AI360Xpert
Beta
Difficulty: EasyStack

Next Greater Element I

Problem in Plain English

For each value in nums1, find that same value in nums2 and report the first larger value to its right, or -1 when none exists. A single decreasing stack over nums2 answers every query in linear time.

Problem Statement

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, where nums1 is a subset of nums2. For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1.

Return an array ans of length nums1.length such that ans[i] is the next greater element as described above.

The answer is the first larger value by position, not the largest value in the suffix or the smallest larger value. Follow-up: solve in O(m+n)O(m + n) time, where m = nums1.length and n = nums2.length.

Constraints

  • 1 <= nums1.length <= nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 10^4
  • All integers in nums1 and nums2 are unique.
  • All the integers of nums1 also appear in nums2.

Examples

Example 1

Input
nums1 = [4,1,2], nums2 = [1,3,4,2]
Output
[-1,3,-1]

4 has no greater element to its right in nums2. 1 is followed by 3. 2 sits at the end with nothing after it.

Example 2

Input
nums1 = [2,4], nums2 = [1,2,3,4]
Output
[3,-1]

2 is followed by 3. 4 is the last element, so nothing greater follows it.

Example 3

Input
nums1 = [0], nums2 = [0]
Output
[-1]

At the smallest valid size, the only value has no position to its right. Zero is an ordinary query value.

Example 4

Input
nums1 = [2,1,5], nums2 = [2,1,5,3,4]
Output
[5,5,-1]

For 2 and 1, the first larger value encountered is 5. Although 3 is smaller than 5 and larger than either query, it occurs later and is not the answer. The maximum value 5 has no greater value to its right.

Intuition

A value waits until the first larger value arrives to its right. In Example 1, 1 meets 3, then 3 meets 4; 4 and the final 2 never meet a larger value. Keep unresolved values in decreasing order on a stack: a new value can settle several smaller waiting values at once. Because all values are unique, a map keyed by value can later answer nums1 in its own order.

Approaches

Scan Each Target

Solution Details

Reveal Scan Each Target: intuition, complexity, and code

Hints

Hint 1
Each answer only depends on what sits to the right of the value inside nums2.
Hint 2
While scanning nums2 left to right, keep the values that have not yet met a greater element.
Hint 3
A decreasing stack plus a value-to-answer map resolves every query in one pass over nums2.

Edge Cases

  • A query for the last element of nums2, such as 2 in Example 1, always answers -1 since nothing follows it.
  • A strictly decreasing nums2 leaves every value unresolved, so every query answers -1.
  • A strictly increasing nums2 resolves every value immediately against its right neighbour.
  • Boundary values 0 and 10000 are legal; a membership test must not confuse zero with an absent key.
  • A single arrival can pop many waiting values, such as 5 after [2,1]; keep popping until the decreasing order is restored.

Common Mistakes and Interview Tips

  • Searching from the left of the value instead of strictly to its right, which can return an element that comes before it.
  • Accepting an equal value as the answer instead of requiring a strictly greater element.
  • Forgetting the -1 default for values that stay on the stack, which drops the queries with no greater element.
  • Popping only one smaller entry with if instead of while leaves earlier queries unresolved: in [2,1,5], arrival 5 settles both 1 and 2.
  • Returning the largest suffix value or the smallest larger value ignores position: [2,5,3] must give 5 for query 2.
  • Reusing a value-keyed map when duplicate values are allowed is unsafe; this problem guarantees uniqueness, while duplicate variants need index-based tracking.

Key Takeaway

A monotonic stack resolves next-greater questions in one pass: smaller values wait on the stack until the arrival that settles them.