Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Next Greater Element I

Easy
stackmonotonic-stackhash-map

nums1 is a subset of nums2 (all values distinct). For each element of nums1, find the first element greater than it appearing to its right in nums2, or −1 if none exists.

Input

  • Line 1: space-separated integers — nums1
  • Line 2: space-separated integers — nums2

Output: the answers for nums1, space-separated.

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

Nothing right of 4 is bigger; 3 follows 1; nothing follows 2.

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

Constraints

  • 1 ≤ nums1.length ≤ nums2.length ≤ 1000
  • All values in nums2 are distinct; every nums1 value appears in nums2.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.