Dynamic Programming21 sections · 916 units
Open in Course

Vocabulary - Prefix Sum

The definition

A prefix sum array stores cumulative sums. Define pref[i]pref[i] as the sum of the first ii elements: pref[0]=0pref[0] = 0 pref[i]=a[1]+a[2]++a[i]pref[i] = a[1] + a[2] + \ldots + a[i] for i1i \ge 1 For example, if a=[3,1,4,1,5]a = [3, 1, 4, 1, 5], then pref=[0,3,4,8,9,14]pref = [0, 3, 4, 8, 9, 14].

Notice pref[0]=0pref[0] = 0 is a boundary case. It represents "sum of zero elements." This makes the formula for range sums cleaner, as you'll see next.