Pregunta de entrevista de Microsoft

Algorithm question in mid to hard level