Back to course home
0% completed
Vote For New Content
Next Greater Element (easy)
Problem Statement
Given two integer arrays nums1
and nums2
, return an array answer
such that answer[i]
is the next greater number for every nums1[i]
in nums2
.
The next greater element for an element x
is the first element to the right of x
that is greater than x
. If there is no greater number, output -1
for that number.
The numbers in nums1
are all present in nums2
.
Examples
-
- Input:
nums1 = [4,2,6]
,nums2 = [6,2,4,5,3,7]
- Output:
[5,4,7]
- Explanation: The next greater number for 4 is 5, for 2 is 4, and for 6 is 7 in
nums2
.
- Input:
-
- Input:
nums1 = [9,7,1]
,nums2 = [1,7,9,5,4,3]
- Output:
[-1,9,7]
- Explanation: The next greater number for 9 does not exist, for 7 is 9, and for 1 is 7 in
nums2
.
- Input:
-
- Input:
nums1 = [5,12,3]
,nums2 = [12,3,5,4,10,15]
- Output:
[10,15,5]
- Explanation: The next greater number for 5 is 10, for 12 is 15, and for 3 is 5 in
nums2
.
- Input:
Constraints:
1 <= nums1.length <= nums2.length <= 1000
- 0 <= nums1[i], nums2[i] <= 10<sup>4</sup>
- All integers in
nums1
andnums2
areunique
. - All the integers of
nums1
also appear innums2
.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
On this page
Problem Statement
Try it yourself