Pergunta de entrevista da empresa Amazon

Stack question based on next greater element.