Pergunta de entrevista da empresa Google

how to solve the problem and time space complexity