The problem. You are given two arrays of distinct numbers, nums1 and nums2, where every number in nums1 also appears in nums2. For each x in nums1, find x in nums2 and return the first number to its right that is greater than `x`. If there isn't one, the answer is -1.
Input: nums1 = [4, 1, 2], nums2 = [1, 3, 4, 2]
Output: [-1, 3, -1]
Input: nums1 = [2, 4], nums2 = [1, 2, 3, 4]
Output: [3, -1]"The first bigger number to the right" is the next greater element — a monotonic stack question. nums1 only says which answers to report.
Free account
Sign up to read the rest of this lesson: 5 more sections, 3 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come