Pergunta de entrevista da empresa Hopper

Solve this problem on some terrible online IDE