C# |  Beats 100.00% | Simple Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to find the second greater element for each element in the array nums. For 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to find the second greater element for each element in the array nums. For each element, the second greater element is defined as:\n\nThe next element that is greater than the current element, and\nThere is exactly one element in between that is also greater than the current element.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Use Two Stacks:\n\n   - indexStack: Keeps track of indices where the first greater element has not been found yet.\n   - secondGreaterStack: Keeps track of indices where the second greater element might be found.\n\n2. Iterate Through the Array:\n\n   - For each element, update result for indices in secondGreaterStack if the current element is greater than the element at the index on top of secondGreaterStack.\n   - Transfer indices from indexStack to tempStack where the current element is greater, then move them back to secondGreaterStack.\n\n3. Update the Stacks:\n\n   - Push the current index onto indexStack.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n   - O(n): Each element is pushed and popped from each stack at most once. Since there are three stacks (indexStack, secondGreaterStack, and tempStack), each operation (push or pop) takes constant time, leading to an overall linear time complexity.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n   - O(n): The space complexity is dominated by the size of the stacks used. Since the maximum number of indices that can be stored in any stack is proportional to the length of the array, the space complexity is linear.\n\n# Code\n```\npublic class Solution {\n    public int[] SecondGreaterElement(int[] nums) {\n        int n = nums.Length;\n        int[] result = new int[n];\n        Array.Fill(result, -1);\n\n        Stack<int> indexStack = new Stack<int>();\n        Stack<int> secondGreaterStack = new Stack<int>();\n        Stack<int> tempStack = new Stack<int>();\n\n        for (int i = 0; i < n; i++) {\n            // While secondGreaterStack is not empty and nums[i] is greater than the element at the top of secondGreaterStack\n            while (secondGreaterStack.Count > 0 && \n                    nums[secondGreaterStack.Peek()] < nums[i]) {\n                result[secondGreaterStack.Pop()] = nums[i];\n            }\n\n            // Move elements from indexStack to tempStack where nums[i] is greater\n            while (indexStack.Count > 0 && nums[indexStack.Peek()] < nums[i]) {\n                tempStack.Push(indexStack.Pop());\n            }\n\n            // Move elements from tempStack back to secondGreaterStack\n            while (tempStack.Count > 0) {\n                secondGreaterStack.Push(tempStack.Pop());\n            }\n\n            // Push the current index onto indexStack\n            indexStack.Push(i);\n        }\n\n        return result;       \n    }\n}\n```
['Array', 'Stack', 'Monotonic Stack', 'C#']