The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.

You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.

For each 0 &lt;= i &lt; nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1.

Return an array ans of length nums1.length such that ans[i] is the next greater element as described above.

&nbsp;
Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2]
Output: [-1,3,-1]
Explanation: The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.


Example 2:

Input: nums1 = [2,4], nums2 = [1,2,3,4]
Output: [3,-1]
Explanation: The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.


&nbsp;
Constraints:


	1 &lt;= nums1.length &lt;= nums2.length &lt;= 1000
	0 &lt;= nums1[i], nums2[i] &lt;= 104
	All integers in nums1 and nums2 are unique.
	All the integers of nums1 also appear in nums2.


&nbsp;
Follow up: Could you find an O(nums1.length + nums2.length) solution?
class Solution:
    def nextGreaterElement(self, nums1, nums2):
        dic, stack = {}, []
        
        for num in nums2[::-1]:
            while stack and num > stack[-1]:
                stack.pop()
            if stack:
                dic[num] = stack[-1]
            stack.append(num)
            
        return [dic.get(num, -1) for num in nums1]
class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
       HashMap<Integer, Integer> map = new HashMap<>();
       Stack<Integer> stack = new Stack<>();
        int[] ans = new int[nums1.length];
        for(int i = 0; i < nums2.length; i++){
            while(!stack.isEmpty() && nums2[i] > stack.peek()){
                int temp = stack.pop();
                map.put(temp, nums2[i]);
            }
            stack.push(nums2[i]);
        }
        for(int i = 0; i < nums1.length; i++){
           ans[i] = map.getOrDefault(nums1[i], -1);
        }
        return ans;
    }
}
class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        vector<int>vc;
        int len1=nums1.size();
        int len2=nums2.size();
        unordered_map<int,int>ump;
        int e,f;
        for(e=0;e<len2;e++)
        {
            for(f=e+1;f<len2;f++)
            {
                if(nums2[f]>nums2[e])
                {
                    ump[nums2[e]]=nums2[f];
                    break;
                }

            }
            if(f==len2) ump[nums2[e]]=-1;

        }
        unordered_map<int,int>::iterator it;
        for(int e=0;e<len1;e++)
        {
            it=ump.find(nums1[e]);
            if(it!=ump.end())
            {
                vc.push_back(it->second);
            }
        }

        return vc;

    }
};
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var nextGreaterElement = function(nums1, nums2) {
    // [Value, Index] of all numbers in nums2
    const indexMap = new Map()
    for(let i = 0; i < nums2.length; i++){
        indexMap.set(nums2[i], i)
    }

    // Stores the next greatest elements
    let result = []

    // Iterate through all the numbers of interest. Remember that all numbers in nums1 are present in nums2.
    for(let num of nums1){

        // Check to see those numbers were present in the nums2 indexMap
        if(indexMap.has(num)){

            // If they were, we must find the next greatest element.
            // Set it to -1 by default in case we cannot find it.
            let nextGreatest = -1

            // Iterate through all numbers in nums2 to the right of the index of the target number. (index of the target + 1)
            for(let i = indexMap.get(num) + 1; i < nums2.length; i++){
                // Check to see if the current number is greater than the target.
                if(nums2[i] > num){
                    // If it is, this is the next greatest element.
                    nextGreatest = nums2[i]
                    // Break the loop.
                    break;
                }
            }
            // Add the next greatest element (if found). Otherwise, add -1 (default)
            result.push(nextGreatest)
        }
    }

    // Return the array of next greatest elements.
    return result
};
Next Greater Element I