Pergunta de entrevista da empresa Amazon

A question on prefix sum which was very hard to solve.