Next Element Greater than Subset

Find the next greater element for each value in the subset array.

Statement

Given two integer arrays, nums1 and nums2, where nums1 is a subset of nums2, find the next greater element of some element x in nums2 that is to the right of x in the same array. If there is no next greater element, then the answer is 1-1.

Constraints

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

Example

The value at the first index in nums1 is located in nums2 at the second index and we have to find an element that is greater in value and closest to it on its right. There is only one element to the right of the second index, and its value (2) is less than the value (4) at the second index. Hence, we return -1 in this case.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.