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.
- Iterate through
nums1. - Find the index of the item in
nums2. - Loop to the right of that index.
- Print the first greater element. If none exists, print -1.
01EXAMPLE 1
Input
nums1=[4,1,2], nums2=[1,3,4,2]Output
-1
3
-1Explanation: 4: none. 1: 3. 2: none.
Constraints
- Implement efficiently.
ArraysStack
JavaScriptSystem handles I/O — write your function only
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