Google Interview Question

how to solve the problem and time space complexity