Pergunta de entrevista da empresa RAM

time complexities and gave a question and asked both brute force and optimised approach