Create an array prefix where prefix[i] is the sum of all elements from index 0 to i. This is a cumulative sum.
For nums=[−2,0,3,−5,2,−1], compute prefix=[−2,−2,1,−4,−2,−3]. Each entry adds the current element to the previous sum.
To find the sum from i to j, compute prefix[j]−prefix[i−1] (or just prefix[j] if i=0). This uses the telescoping property: intermediate terms cancel.