count = 0 # lets say count == 3, it means until this idx, if 0 appeared x time, 1 appeared x+3 time res = 0 # So, once count == 3 appears at the other position again, for i in range(len(nums)): # ...
Explanation: Use a stack to keep track of elements for which we haven't found the next greater element yet. Iterate through the array, and for each element, pop elements from the stack until you find ...