Pregunta de entrevista de OfferUp

Leetcode question but it is a hard one.