Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

88. Next Greater Element I

EasyDay 26 of 60Stack & Queue

You get two arrays of distinct integers, nums1 and nums2, where every value of nums1 also appears in nums2. For each x in nums1, find where x sits in nums2 and return the first value to its right in nums2 that is greater than x, or -1 if there is none. Return the answers in the order of nums1.

Example 1
Input: nums1 = [4,1,2], nums2 = [1,3,4,2]
Output: [-1,3,-1]
Nothing right of 4 is bigger; right of 1 the first bigger value is 3; nothing right of 2.
Example 2
Input: nums1 = [2,4], nums2 = [1,2,3,4]
Output: [3,-1]
Constraints
  • 1 <= nums1.length <= nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 10^4
  • All values are distinct; nums1 is a subset of nums2
Hints
Frequently asked at: Amazon, Bloomberg, Adobe, Zoho
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min