Pergunta de entrevista da empresa Microsoft

Algorithm question in mid to hard level