Pregunta de entrevista de Walmart

Nothing difficult. Just basic algorithms.