Pregunta de entrevista de Palantir Technologies

Algorithm question on hackerrank. Difficult at the time, found an easy solution afterwards. Definitely take some time to think about the problem before rushing in and writing code. The question appeared difficult but was quite easy.