Data Structures19 sections · 729 units
Open in Course

Vocabulary - Difference Array

Inverse of prefix sum

A difference array stores the difference between consecutive elements: diff[i]=arr[i]arr[i1]diff[i] = arr[i] - arr[i-1] With diff[0]=arr[0]diff[0] = arr[0].

Notice: if you take the prefix sum of the difference array, you get back the original array. Difference arrays and prefix sums are inverses of each other. This inverse relationship is what makes O(1)O(1) range updates possible.