Cumulative sum at each index in JavaScript

In JavaScript, calculating the cumulative sum (also known as running sum or prefix sum) at each index means computing the sum of all elements from the beginning of the array up to the current position. This is a common array operation used in algorithms and data analysis.

What is Cumulative Sum?

The cumulative sum is the calculation of the sum of a series of numbers up to a given index. Each element in the resulting array represents the sum of all previous elements including the current element.

For example, given an array [1, 2, 3, 4, 5], the cumulative sum at each index would be:

  • At index 0: 1
  • At index 1: 1 + 2 = 3
  • At index 2: 1 + 2 + 3 = 6
  • At index 3: 1 + 2 + 3 + 4 = 10
  • At index 4: 1 + 2 + 3 + 4 + 5 = 15

The resulting cumulative sum array would be [1, 3, 6, 10, 15].

Algorithm

To calculate the cumulative sum at each index:

  1. Create a function that takes an array as input
  2. Initialize an empty result array and a running sum variable
  3. Iterate through each element in the input array
  4. Add the current element to the running sum
  5. Push the current sum to the result array
  6. Return the result array containing cumulative sums

Using a For Loop

function cumulativeSum(arr) {
   let result = [];
   let sum = 0;

   for (let i = 0; i < arr.length; i++) {
      sum += arr[i];
      result.push(sum);
   }

   return result;
}

// Usage example
const inputArray = [11, 21, 31, 41, 51];
const cumulativeSums = cumulativeSum(inputArray);
console.log("Input array:", inputArray);
console.log("Cumulative sums:", cumulativeSums);
Input array: [ 11, 21, 31, 41, 51 ]
Cumulative sums: [ 11, 32, 63, 104, 155 ]

Using Array.map() Method

function cumulativeSumMap(arr) {
   let sum = 0;
   return arr.map(num => sum += num);
}

// Example with different data
const numbers = [5, 10, 15, 20];
const result = cumulativeSumMap(numbers);
console.log("Original:", numbers);
console.log("Cumulative:", result);
Original: [ 5, 10, 15, 20 ]
Cumulative: [ 5, 15, 30, 50 ]

Using Array.reduce() Method

function cumulativeSumReduce(arr) {
   let sum = 0;
   return arr.reduce((acc, num) => {
      sum += num;
      acc.push(sum);
      return acc;
   }, []);
}

// Testing with negative numbers
const mixedNumbers = [1, -2, 3, -4, 5];
const cumulativeResult = cumulativeSumReduce(mixedNumbers);
console.log("Mixed numbers:", mixedNumbers);
console.log("Cumulative sum:", cumulativeResult);
Mixed numbers: [ 1, -2, 3, -4, 5 ]
Cumulative sum: [ 1, -1, 2, -2, 3 ]

Comparison of Methods

Method Readability Performance Memory Usage
For Loop High Best Efficient
Array.map() Medium Good Moderate
Array.reduce() Medium Good Moderate

Time and Space Complexity

Time Complexity: O(n) - where n is the length of the input array, as we need to iterate through each element once.

Space Complexity: O(n) - we create a new array to store the cumulative sums.

Conclusion

Calculating cumulative sum at each index is efficiently achieved using iteration with O(n) time complexity. The for loop approach offers the best performance, while functional methods like map() and reduce() provide more expressive alternatives for different coding styles.

Updated on: 2026-03-15T23:19:00+05:30

982 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements