Google Interview Question

Improve the complexity of the algorithm