Pergunta de entrevista da empresa CNN

Algorithm question in JavaScript.