Pregunta de entrevista de Uber

time Complexity, How i would optimize from original solution.