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

<p>The <strong>next greater number</strong> of a number <code>x</code> 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&#39;t exist, return <code>-1</code> for this number.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,1]
<strong>Output:</strong> [2,-1,2]
Explanation: The first 1&#39;s next greater number is 2; 
The number 2 can&#39;t find next greater number. 
The second 1&#39;s next greater number needs to search circularly, which is also 2.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,3]
<strong>Output:</strong> [2,3,4,-1,4]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    void print(vector<int> &input) {
        for(int i = 0;i < input.size();i++) {
            cout << input[i] << " ";
        }
        cout << endl;
    }

    vector<int> nextGreaterElements(vector<int>& input) {
        int size = input.size();
        auto itr = max_element(input.begin(), input.end());
        int index = itr - input.begin();

        vector<int> helper(index+1);
        copy(input.begin(), input.begin() + index+1, helper.begin());

        for(int i = index+1;i < input.size();i++) {
            input[i-index-1] = input[i];
        }
        
        int k = 0;
        for(int i = size-1-index;i < size;i++) {
            input[i] = helper[k];
            k++;
        }

        stack<int> s;
        vector<int> ans(size);
        for(int i=size-1;i >=0;i--) {
            while(!s.empty() && s.top() <= input[i]) {
                s.pop();
            }

            if(s.empty()) {
                ans[i] = -1;
            }else{
                ans[i] = s.top();
            }

            s.push(input[i]);
        }

        int firstSize = size-1-index;
        vector<int> newHelper(size);
        copy(ans.begin(), ans.begin() + firstSize, newHelper.begin());

        k = 0;
        for(int i = firstSize;i < size;i++) {
            ans[i-firstSize] = ans[i];
        }

        int secondSize= index+1;
        for(int i = index+1;i < size;i++) {
            ans[i] = newHelper[k++];
        }

        return ans;
    }
};
27500