Pergunta de entrevista da empresa Amazon

Problem similar to something in cracking the coding interview