C++ Different Solution
\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int> &nums)\n{\n    stack<int> s;\n    int idx = 0;\n    int max = nums[0];\n    for (in
```\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int> &nums)\n{\n    stack<int> s;\n    int idx = 0;\n    int max = nums[0];\n    for (int i = 0; i < nums.size(); i++) // Finding the maximum element in the array\n        if (nums[i] > max)\n        {\n            max = nums[i];\n            idx = i;\n        }\n\n    vector<int> greater(nums.size());\n    greater[idx] = -1;\n    s.push(nums[idx]);\n\n// Finding the next greater element from the starting till the maximum element with normal apprach\n    for (int i = idx - 1; i >= 0; i--) \n    {\n        while (s.size() > 0 && nums[i] >= s.top()) s.pop();\n        greater[i] = s.size() == 0 ? -1 : s.top();\n        s.push(nums[i]);\n    }\n\n// Now we already have the previous elements from the maximum element in the stack \n// and we will find the next greater element for remaining elements using same stack with normal approach\n    for (int i = nums.size() - 1; i > idx; i--)\n    {\n        while (s.size() > 0 && nums[i] >= s.top()) s.pop();\n        greater[i] = s.size() == 0 ? -1 : s.top();\n        s.push(nums[i]);\n    }\n    return greater;\n}\n};\n```
[]