Understanding the Concept of Prefix Sum Array

Ateev Duggal
5 min readJul 10, 2023
Prefix Sum Array

In the realm of computer programming and algorithmic problem-solving, the concept of prefix sum array has proven to be a powerful tool. Whether you’re a seasoned programmer or just starting, understanding and utilizing prefix sum arrays can greatly enhance your ability to solve various types of problems efficiently. In this comprehensive guide, we will delve into the construction and applications of Prefix Sum Arrays So, let’s dive in and unlock the potential of prefix sum arrays.

Index

  1. What is Prefix Sum Array?
  2. Constructing Prefix Sum Array
  3. Understanding Prefix Sum Array with an Example
  4. Conclusion

What is Prefix Sum Array?

A prefix sum array, also known as a cumulative sum array, is a derived array that stores the cumulative sum of elements in a given array. Each element in the prefix sum array represents the sum of all elements up to that index in the original array. It acts as a precursor to answering queries related to cumulative sums, allowing for fast and efficient computations. It also reduces time complexity giving us a way out of TLE.

Constructing Prefix Sum Array

To construct a one-dimensional prefix sum array, we iterate through the input array and compute the cumulative sum up to each index. The prefix sum value at index i is the sum of all elements from index 0 to i in the original array. Here are the steps involved in the construction of the prefix sum array:

  1. Initialize an array, say prefixSum[], of the same length as the input array.
  2. Set the first element of the prefixSum[] as the same as the first element of the input array.
  3. Iterate through the input array starting from the second element.
  4. For each element, calculate the prefix sum by adding the current element with the prefix sum value of the previous index.
  5. Assign the prefix sum value to the corresponding index in the prefixSum array.
  6. Repeat steps 3–5 until the end of the input array is reached.

Example Time

Let’s take an example to understand. Suppose we have an input array A = [3, 1, 5, 2, 4]. We will walk through the steps of constructing the prefix sum array using this example.

Step 1: Create an array, say pf[] which has the same size as the input array which in this case is 5.

Step 2: Initialize the prefix sum array and assign the first element of the input array to it.

Prefix Sum Array: [3, _, _, _, _]

Step 3: Iterate through the input array starting from the second element.

Current Element: 1

Step 4: Calculate the prefix sum by adding the current element with the prefix sum value of the previous index.

Prefix Sum at Index 1 = Prefix Sum at Index 0 + Current Element

Prefix Sum at Index 1 = 3 + 1 = 4

Step 5: Assign the computed prefix sum value to the corresponding index in the prefix sum array.

Prefix Sum Array: [3, 4, _, _, _]

Step 6: Repeat steps 4–5 for the remaining elements of the input array.

Current Element: 5

Prefix Sum at Index 2 = Prefix Sum at Index 1 + Current Element

Prefix Sum at Index 2 = 4 + 5 = 9

Prefix Sum Array: [3, 4, 9, _, _]

Current Element: 2

Prefix Sum at Index 3 = Prefix Sum at Index 2 + Current Element

Prefix Sum at Index 3 = 9 + 2 = 11

Prefix Sum Array: [3, 4, 9, 11, _]

Current Element: 4

Prefix Sum at Index 4 = Prefix Sum at Index 3 + Current Element

Prefix Sum at Index 4 = 11 + 4 = 15

Prefix Sum Array: [3, 4, 9, 11, 15]

Step 7: The construction is complete, and we have the prefix sum array for the given input array.

Prefix Sum Array: [3, 4, 9, 11, 15]

def construct_prefix_sum_array(arr):
n = len(arr)
prefix_sum = [0] * n
prefix_sum[0] = arr[0]
for i in range(1, n):
prefix_sum[i] = prefix_sum[i — 1] + arr[i]
return prefix_sum
public class PrefixSumArray {
public static int[] constructPrefixSumArray(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;
}
public static void main(String[] args) {
int[] inputArray = {3, 1, 5, 2, 4};
int[] prefixSumArray = constructPrefixSumArray(inputArray);
for (int num : prefixSumArray) {
System.out.print(num + “ “);
}
}
}
#include <iostream>
#include <vector>
std::vector<int> constructPrefixSumArray(std::vector<int>& arr) {
int n = arr.size();
std::vector<int> prefixSum(n);
prefixSum[0] = arr[0];
for (int i = 1; i < n; i++) {
prefixSum[i] = prefixSum[i — 1] + arr[i];
}
return prefixSum;
}
int main() {
std::vector<int> inputArray = {3, 1, 5, 2, 4};
std::vector<int> prefixSumArray = constructPrefixSumArray(inputArray);
for (int num : prefixSumArray) {
std::cout << num << “ “;
}
return 0;
}

Time and Space Complexity

The time complexity of the Prefix Sum Array is O(n), where n is the size of the input array. This is because we iterate through the input array once, performing a constant-time operation for each element to calculate the prefix sum.

The space complexity of the Prefix Sum Array is O(n), where n is the size of the input array. This is because we create an additional array of the same size as the input array to store the prefix sum values.

Conclusion

The prefix sum array serves as a powerful tool in optimizing computations involving cumulative sums. By constructing a prefix sum array, we can efficiently answer queries related to range sums, subarray sums, and average calculations. The ability to perform these computations in constant time brings about significant performance improvements, especially for large datasets. Understanding and harnessing the potential of prefix sum arrays equips us with a valuable technique to tackle computational problems more efficiently.

So, next time we encounter problems that require computing cumulative sums, consider leveraging the power of prefix sum arrays. By employing this technique, we can unlock new levels of efficiency and enhance our problem-solving skills in the world of algorithms and data structures.

--

--

Ateev Duggal

I am a front-end developer from India with over one year of experience in freelancing with skills like Git, HTML5, CSS3, Bootstrap 4&5, and React.