Pregunta de entrevista de Oracle

Algorithm question was Medium level leetcode one.