Next Greater Element I

HardAcc. 82.1%
+45 XP 20

Future Outlook

For each element in nums1, find its position in nums2, then search to the right in nums2 for the first larger number.

The Assignment

Your function receives nums1 and nums2.

  1. Iterate through nums1.
  2. Find the index of the item in nums2.
  3. Loop to the right of that index.
  4. Print the first greater element. If none exists, print -1.

01EXAMPLE 1

Inputnums1=[4,1,2], nums2=[1,3,4,2]
Output-1 3 -1

Explanation: 4: none. 1: 3. 2: none.

Constraints

  • Implement efficiently.
ArraysStack
JavaScript
Loading...
1 Hidden

Input Arguments

nums1[4,1,2]
nums2[1,3,4,2]

Expected Output

-1
3
-1

Click RUN to test your solution