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.

Example 1:

Input: nums = [1,2,1]

Output: [2,-1,2]

public class Solution {

public int[] nextGreaterElements(int[] nums) {

int max = Integer.MIN_VALUE;

for (int num : nums) {

max = Math.max(max, num);

}

int n = nums.length;

int[] result = new int[n];

int[] temp = new int[n * 2];

for (int i = 0; i < n * 2; i++) {

temp[i] = nums[i % n];

}

for (int i = 0; i < n; i++) {

result[i] = -1;

if (nums[i] == max) continue;

for (int j = i + 1; j < n * 2; j++) {

if (temp[j] > nums[i]) {

result[i] = temp[j];

break;

}

}

}

return result;

}

}