Pergunta de entrevista da empresa Super Evil Megacorp

I was given a problem with a naive O(n^2) solution, how to optimize it?