Pregunta de entrevista de Marshall Wace

Algorithmic question (LC hard) with multiple edge cases