Dynamic Programming - Part sum of elements JavaScript


In this problem statement, our task is to calculate the sum of all the elements of the array and remove one element at every step and show the resultant array of sum of all the possible subarrays. And implement this problem with the help of Javascript functionalities.

Logic for the given problem

The given problem states that we have to calculate the sum of every element except one element at every step. To solve the given problem we will define an array as input and return the output as an array of sums of elements by deducting one element at each iteration.

And we will use a for loop to iterate over the items of the array and calculate the sum by eliminating the current element using the same method as before. Then add these sums to the new result array. At every stage the current element will be altered.

Algorithm

Step 1 − At the start of the program, declare an array of integers that will be utilized to calculate the sum of all the elements except one element at each step. In our code if we have 5 elements in the array then we will calculate the sum of four elements.

Step 2 − If n is the number of items present in the array, we have to store n-1 item's sum in the array. So to store the sums we will create an array of sums.

Step 3 − In Javascript we have a method called reduce which is used to update all the values of the array by iterating the elements one by one. So we will also use the reduce method to get the sum of all the elements.

Step 4 − And after getting the sum push the sum in the result variable.

Step 5 − Now initiate the for loop and run till the length of the array.

Step 6 − In this phase we will slice one element from the array for getting the sum of n-1 items.

Step 7 − At the last step, push all the resultant sum in the result array and get the output on the console.

Code for the algorithm

// array to calculate the part sum
const arr = [1, 2, 3, 4, 5];

// resultant array 
const sumArray = (arr = []) => arr.reduce((a, b) => a + b, 0);
const sumWithoutOne = (arr = []) => {
   const result = [sumArray(arr)];
   for (let i = 0; i < arr.length; i++) {
      const sum = sumArray(arr.slice(0, i).concat(arr.slice(i + 1)));
      result.push(sum);
   }
   return result;
};
console.log(sumWithoutOne(arr));

Complexity

We can see in our code, we are using a reduce method and it takes O(n) time to execute it. And another function sumWithoutOne is using a for loop and slice method which is also taking O(n) time but slice method is called n times because it is in the for loop so over all time complexity will be O(n^2). And the space complexity is O(n), where n is the number of sums in the new array.

Conclusionary remarks

We have seen in the above algorithm how to get the sum of some part of the array. If an array has n elements, we have calculated the n-1 element’s sum by removing one element at every step from the array and creating a new array which is holding the sum of the n-1 items. So the time complexity for this code is O(n^2) and space complexity is O(n).

Updated on: 18-May-2023

346 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements