Given a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums.

The next greater number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, return -1 for this number.

&nbsp;
Example 1:

Input: nums = [1,2,1]
Output: [2,-1,2]
Explanation: The first 1's next greater number is 2; 
The number 2 can't find next greater number. 
The second 1's next greater number needs to search circularly, which is also 2.


Example 2:

Input: nums = [1,2,3,4,3]
Output: [2,3,4,-1,4]


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 104
	-109 &lt;= nums[i] &lt;= 109


class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        t=len(nums)
        nums+=nums
        l=[-1]*len(nums)
        d={}
        stack=[0]
        for x in range(1,len(nums)):
            #print(x)
            if nums[x]>nums[stack[-1]]:
                while nums[x]>nums[stack[-1]] :
                    l[stack[-1]]=nums[x]
                    stack.pop()
                    if stack==[]:
                        break
                    #print(l)
                stack.append(x)
                    
            else:
                stack.append(x)
        return l[:t]
            
        
class Solution {
    public int[] nextGreaterElements(int[] nums) {
        Stack<Integer>s=new Stack<>();
        for(int i=nums.length-1;i>=0;i--){
            s.push(nums[i]);
        }
        for(int i=nums.length-1;i>=0;i--){
            int num=nums[i];
            while(!s.isEmpty() && s.peek()<=nums[i]){
                s.pop();
            }
            nums[i]=s.empty()?-1:s.peek();
            s.push(num);
        }
        return nums;
    }
}
class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        int n = nums.size();
        vector<int> res;
        
        for(int i = 0; i < n; i++){
            int j = i + 1;
            if(j == n) j = 0;
            int next = -1;
            
            while(j != i){
                if(nums[j] > nums[i]){
                    next = nums[j];
                    break;
                }
                j++;
                if(j == n) j = 0;
            }
            res.push_back(next);
        }
        return res;
    }
};
var nextGreaterElements = function(nums) {
    const len = nums.length;
    const ans = new Array(len).fill(-1);
    const stack = [];
    for(let i = 0; i < len; i++) {
        if(i == 0) stack.push([i, nums[i]]);
        else {
            while(stack.length && stack.at(-1)[1] < nums[i]) {
                ans[stack.pop()[0]] = nums[i];
            }
            stack.push([i, nums[i]]);
        }
    }
    for(let num of nums) {
        while(stack.length && stack.at(-1)[1] < num) {
            ans[stack.pop()[0]] = num;
        }
    }
    return ans;
};
Next Greater Element II