Question d’entretien chez Meta

Solve this algorithm problem in an optimised way