Sort indices + SortedList
For the purpose of this question elements in the array smaller than the one in question are invisible. So, sort the indices by the reverse order of their values
For the purpose of this question elements in the array smaller than the one in question are invisible. So, sort the indices by the reverse order of their values. As we go through the indices one by one, keep them in a `SortedList`. If there are >=2 indices greater than the current one, use the second one. Otherwise, -1.\n\n(`sorted` in python is stable so indices whose values are the same go in order and indices to the right won\'t be in `sl`)\n```\nfrom sortedcontainers import SortedList\n\nclass Solution:\n    def secondGreaterElement(self, nums: List[int]) -> List[int]:\n        n = len(nums)\n        indices = sorted(range(n), key=lambda i: nums[i], reverse=True)\n        sl = SortedList()\n        ans = [None] * n\n        for i in indices:\n            index = sl.bisect_right(i)\n            if index + 1 < len(sl):\n                ans[i] = nums[sl[index + 1]]\n            else:\n                ans[i] = -1\n            sl.add(i)\n        return ans\n```
['Python']