Cisco Interview Question

how to improve time complexity of program