Google interview question

Improve the complexity of the algorithm