Pergunta de entrevista da empresa H E B

Standard algorithm question, nothing to difficult.