Partial sum in array of arrays JavaScript


In the given problem statement our task is to get the partial sum in an array of arrays with the help of Javascript functionalities. So we will calculate the sum of rows and give the result.

Understanding the Problem

The problem at hand is to compute the partial sum of an array of arrays. So first understand what an array of arrays is! Array of arrays means each item represents an array itself. For example see the below array of arrays:

[

[11, 12, 13],

[14, 15, 16],

[17, 18, 19]

]

The partial sum at position (i, j) is the sum of all the elements from the top left corner of the input array to the element at position (i, j).

Algorithm

Step 1: As we have to compute the partial sum in an array of arrays, first we will create a function to compute the partial sums of the given array and give it name as sumOfSubarrays. And pass a parameter of array as arr.

Step 2: After defining the function Inside the function first check the input array is empty if it is empty then return.

Step 3: Calculate the number of rows and columns in the input array.

Step 4: Create a 2D array to store the result of partial sums.

Step 5: Iterate over each element of the input array and compute the partial sum for every item as per the position and previously computed partial sums.

Step 6: The partial sum at position (i, j) is computed with the formula.

Step 7: The formula uses the values of the previous row and column. And also subtract the value in the top-left corner to avoid the double counting.

Example

function sumOfSubarrays(arr) {
   if (arr.length === 0) return [];

   const numRows = arr.length;
   const numCols = arr[0].length;

   // Create a 2D array to store the partial sums
   const partialSums = new Array(numRows);
   for (let i = 0; i < numRows; i++) {
      partialSums[i] = new Array(numCols);
   }

   // Compute the partial sums
   for (let i = 0; i < numRows; i++) {
      for (let j = 0; j < numCols; j++) {
         if (i === 0 && j === 0) {
            partialSums[i][j] = arr[i][j];
         } else if (i === 0) {
            partialSums[i][j] = partialSums[i][j - 1] + arr[i][j];
         } else if (j === 0) {
            partialSums[i][j] = partialSums[i - 1][j] + arr[i][j];
         } else {
            partialSums[i][j] = partialSums[i - 1][j] + partialSums[i][j - 
1] - partialSums[i - 1][j - 1] + arr[i][j];
         }
      }
   }

   return partialSums;
}

// Example usage
const array = [
   [1, 2, 3],
   [4, 5, 6],
   [7, 8, 9]
];

const result = sumOfSubarrays(array);
console.log(result);

Output

[ [ 1, 3, 6 ], [ 5, 12, 21 ], [ 12, 27, 45 ] ]

Complexity

The time complexity to find the partial sum in an array of arrays is O(n) for the above function to get the partial sums of the given array. And the space complexity for the function is also O(n) as we are storing the sums of the given array with the same length.

Conclusion

As we can see in the algorithm we have used a dynamic approach to get the partial sums of the given arrays. And the function is suitable for any size of array.

Updated on: 16-Aug-2023

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements