Linking Tao
Techniques

PrefixSum

Technique for storing a cumulative sum of elements.

preffix.png

The PrefixSum technique consists of creating a final array with the previous sum of elements in each position of the array. It is similar to representing the snowball effect using an algorithm.

prefixSum[i] = arr[0] + arr[1] ++ arr[i]

The power of PrefixSum lies
on efficient cumulative calculation.
Optimise repetitive operations.
DRY principle.

It may be used not only
on leetcode problems,
but also for real-life
operations.

As the Ubermensch follows,
the PrefixSum represents
the continuous life process
for self-overcoming.

Is today stronger than the past, taken as a whole?


Example code

class PrefixSumExample
{
    public static int[] ComputePrefixSum(int[] arr)
    {
        int n = arr.Length;
        int[] prefixSum = new int[n];
        prefixSum[0] = arr[0];
        for (int i = 1; i < n; i++)
        {
            prefixSum[i] = prefixSum[i - 1] + arr[i];
        }
        return prefixSum;
    }
    static void Main()
    {
        int[] arr = { 2, 4, 6, 8 };
        int[] prefixSum = ComputePrefixSum(arr);
        Console.WriteLine("Prefix Sum Array:");
        foreach (var sum in prefixSum)
        {
            Console.Write(sum + " ");  // Output: 2 6 12 20
        }
    }
}

Source: Prefix Sums Demystified

On this page