Cumulative sum at each index in JavaScript


In the given problem statement we have to calculate the cumulative sum at each index with the help of Javascript functionalities. So we will use basic Javascript syntax and functions to solve this problem.

What is Cumulative sum ?

The cumulative sum is also known as running sum or prefix sum. So this sum is the calculation of the sum of a series of numbers up to a given index or position. In this process the numbers iteratively add each number in the series to the sum of the previous items. So the resultant new series in which every item shows the sum of all the previous items including the number itself.

To show this concept, let's see an example. Let's say we have an array as [1, 2, 3, 4, 5]. So the cumulative sum at every index will be:

At index 0 - 1

At index 1 - 1 + 2

At index 2: 1 + 2 + 3 = 6

At index 3: 1 + 2 + 3 + 4 = 10

At index 4: 1 + 2 + 3 + 4 + 5 = 15

So the cumulative sum array will be [1, 3, 6, 10, 15]. In this new sequence every item shows the sum of all the items in the given array up to that last index.

Understanding the problem

The problem at hand is to calculate the cumulative sum of the given array. As we have seen above what exactly the cumulative sum is. So in this article we will solve this problem and discuss algorithm, code and complexity for getting the desired result.

Logic for the given problem

To solve the given problem for calculating the cumulative sum at each index, we will iterate the array and add the current item to the running sum of the previous items. So first we will start the initial sum to 0 and iterate over each item in the array. At every index we will add the current item to the running rum and we will store it in the respective index of a new result array.

Algorithm

Step 1: So we have to compute the cumulative sum of the given array so first we will create a function and give it a name as cumulativeSum and in this function we will pass a parameter of array as arr.

Step 2: After creating the above function we will create the blank result array which will be used to store the cumulative sums.

Step 3: And then we will use a variable called sum to store the running sum of the items and initialize it with zero.

Step 4: Now we have defined all the necessary things, after that we are required to iterate each item as num in the given input array. And add num to sum to compute the cumulative sum. And then we will push the current value of the sum variable to the result array we have created above.

Step 5: And finally we will return the value of the result array to show the array of cumulative sums.

Code for the algorithm

Example

//Function to get the cumulative sum
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
const inputArray = [11, 21, 31, 41, 51];
const cumulativeSums = cumulativeSum(inputArray);
console.log(cumulativeSums);

Output

[ 11, 32, 63, 104, 155 ]

Complexity

The time complexity for computing the cumulative sums for every index in the given array is O(n), in which n is the size of the given input array. The reason for this complexity is that the code iterates through the array one time to compute the required sums. And the space complexity is also O(n) because we have created a new array to store the cumulative sums.

Conclusion

The code has successfully shown the cumulative sums at each index in the given array. As we have iterated the given array once to get the cumulative sums at every index with the time complexity of O(n).

Updated on: 14-Aug-2023

602 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements