Google interview question

how to solve the problem and time space complexity