In this function why i is 2n-1?

vector nextGreaterElements(vector &nums)
{
int n = nums.size();
vector res(n);

stack<int> s;

for (int i = 2 * n - 1; i >= 0; i--)
{

    while (!s.empty() && nums[s.top()] <= nums[i % n])
    {
        s.pop();
    }

    res[i % n] = (s.empty() ? -1 : nums[s.top()]);
    s.push(i % n);
}

return res;

}

we assuming that in array after n-1 th index we have same element again till 2*n-1 th index
this is because array is circular
this is space optimised code

simple approach will be like this
check this code it is easy to understand

if you have more doubts regarding this feel free to ask
i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved :grinning: