AlgoMaster Logo

Next Greater Element I

easyFrequency6 min readUpdated June 23, 2026

Understanding the Problem

We have two arrays. nums1 is a subset of nums2, so every element in nums1 exists somewhere in nums2. For each element in nums1, we find it in nums2, then look to the right in nums2 for the first element that is larger. If nothing larger exists to the right, the answer is -1.

We are not looking for the next greater element within nums1. We find each element's position in nums2 and scan right within nums2. The problem reduces to: for every element in nums2, what is its next greater element? Once we have that mapping, answering queries from nums1 is a lookup.

Key Constraints:

  • 1 <= nums1.length <= nums2.length <= 1000 → With n up to 1000, an O(m*n) scan is at most a million operations, fast enough to pass. The O(n) monotonic stack solution is the more useful one to know.
  • All integers in nums1 and nums2 are unique. → No duplicates means we can map each value to its next greater element in a hash map without worrying about two elements colliding on the same key.
  • nums1 is a subset of nums2 → Every value in nums1 exists in nums2, so locating an element never fails.

Approach 1: Brute Force

Intuition

Do exactly what the problem describes. For each element in nums1, find where it sits in nums2, then scan to the right in nums2 until we find something bigger. If we reach the end without finding anything, the answer is -1.

Because all elements are unique, locating an element in nums2 is a single linear scan, and with n up to 1000 the repeated scanning stays within budget.

Algorithm

  1. Create a result array of the same length as nums1.
  2. For each element nums1[i]:
    • Find the index j in nums2 where nums2[j] == nums1[i].
    • From index j + 1 to the end of nums2, look for the first element greater than nums1[i].
    • If found, store that element in the result. Otherwise, store -1.
  3. Return the result array.

Example Walkthrough

1Query 1: Find 4 in nums2. Scan from left.
0
1
j
1
3
2
4
3
2
1/7

Code

The bottleneck is that we re-scan nums2 from scratch for each query, even though the next greater element relationship is a property of nums2 alone. The next approach precomputes that relationship for every value in nums2 in a single pass, then answers each query with a lookup.

Approach 2: Monotonic Stack + Hash Map

Intuition

The next greater element relationship is determined by nums2 alone. So we precompute a mapping from each element of nums2 to its next greater element, then answer every query from nums1 with a hash map lookup. Computing the next greater element for every element in one pass is the monotonic stack pattern.

Process nums2 from left to right, keeping a stack of elements that have not yet found their next greater element. These elements stay in decreasing order on the stack: any element pushed on top of another is smaller than it, otherwise the one below would have been popped first. When the current element is larger than the stack top, the current element is the next greater element for that top. Pop it, record the mapping, and compare against the new top. Keep popping while the top is smaller than the current element, then push the current element.

After the whole array is processed, any elements left on the stack never found a larger element to their right, so their answer is -1.

Algorithm

  1. Create an empty stack and an empty hash map.
  2. Iterate through nums2 from left to right:
    • While the stack is not empty and the current element is greater than the top of the stack:
      • Pop the top element.
      • Record in the hash map: popped element -> current element (this is its next greater element).
    • Push the current element onto the stack.
  3. Any elements remaining on the stack have no next greater element (they map to -1 by default).
  4. For each element in nums1, look up its next greater element in the hash map. If it's not in the map, the answer is -1.
  5. Return the result array.

Example Walkthrough

1Start processing nums2. Current element: 1
0
1
current
1
3
2
4
3
2
1/12

Code