Domanda di colloquio di Google

how to solve the problem and time space complexity