in java with O(n) Time Complexity
Approach\n Describe your approach to solving the problem. \n#### 1. Initialize Variables :\n- The array length is stored in n.\n- A result array res of size n i
# Approach\n<!-- Describe your approach to solving the problem. -->\n#### 1. Initialize Variables :\n- The array length is stored in n.\n- A result array res of size n is initialized with all elements set to -1. This array will hold the final answers.\n- Two stacks, firstStack and secondStack, are used to track indices of elements while processing the first and second greater elements.\n#### 2. Iterate through the Array:\n- The main loop runs through each index i of the input array nums.\n#### 3. Process the Second Greater Elements:\n- While the secondStack is not empty and the current element nums[i] is greater than the element at the index stored at the top of secondStack, pop from secondStack and update res for that index with nums[i]. This indicates that we have found the second greater element for that index.\n#### 4. Process the First Greater Elements:\n- Use a temporary stack temp to move elements from firstStack where the current element is greater. These elements are then pushed to secondStack, as they are potential candidates for the second greater element.\n#### 5. Push Current Index to firstStack:\n\n- After processing, push the current index i to firstStack as it is now the element for which we need to track future greater elements.\n#### 6. Final Result:\n\n- The loop completes, and the res array contains the second greater elements for each index. If no such element exists for a particular index, it remains as -1.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n# For Beginners (Time Limit Exceede) \u23F3\u274C\u274C\n```\nclass Solution {\n    public int[] secondGreaterElement(int[] nums) {\n        int n = nums.length;\n        if(n<2){\n            nums[0]=-1;\n            return nums;\n        }\n        for(int i = 0 ;i<n-2;i++){\n            nums[i] = forSecondHigh(nums,i,n);\n        }\n        nums[n-1] = -1;\n        nums[n-2] = -1;\n        return nums;\n    }\n\n    public int forSecondHigh(int[] nums ,int index , int n){\n        int count=0;\n        for(int i = index ; i<n;i++){\n            if(nums[index]<nums[i]){\n                count++;\n            }\n            if(count==2){\n                return nums[i];\n            }\n        }\n        return -1;\n    }\n}\n```\n\n# Solution In java\n```\nclass Solution {\n    public int[] secondGreaterElement(int[] nums) {\n        int n = nums.length;\n        int[] res = new int[n];\n        Arrays.fill(res, -1);\n\n        // First stack to track indices where we need to find the first greater element\n        Stack<Integer> firstStack = new Stack<>();\n        // Second stack to track indices where we need to find the second greater element\n        Stack<Integer> secondStack = new Stack<>();\n\n        for (int i = 0; i < n; i++) {\n            // Process second greater elements\n            while (!secondStack.isEmpty() && nums[secondStack.peek()] < nums[i]) {\n                res[secondStack.pop()] = nums[i];\n            }\n\n            // Process first greater elements and move them to the second stack\n            Stack<Integer> temp = new Stack<>();\n            while (!firstStack.isEmpty() && nums[firstStack.peek()] < nums[i]) {\n                temp.push(firstStack.pop());\n            }\n            while (!temp.isEmpty()) {\n                secondStack.push(temp.pop());\n            }\n\n            // Push current index to the first stack\n            firstStack.push(i);\n        }\n\n        return res;\n    }\n}\n```
['Array', 'Stack', 'Java']