Pregunta de entrevista de Meta

Can you write a script to solve this problem