Pergunta de entrevista da empresa Goldman Sachs

Medium difficult leetcode style problem.