 All Problems
Next Greater Element I
easy
stack
array
hash map
monotonic stack
amazon
google
facebook

The next greater element of some element x in an array is the first greater element that is to its right 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 element nums1[i], find the index j such that nums2[j] == nums1[i] 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.

Example 1:

Input:
4 1 2
1 3 4 2
Output: -1 3 -1

Example 2:

Input:
2 4
1 2 3 4
Output: 3 -1

Input format: First line: nums1. Second line: nums2.

Output format: Space-separated next greater elements.

Run to check your code against the sample cases, or submit to run every case