Pregunta de entrevista de BlackBelt Technology

How would you solve this problem with an algorithm? Can we make it more efficient?