Calculating the average for each subarray separately and then return the sum of all the averages in JavaScript


In the given problem statement we are asked to calculate the average for each subarray separately and then return the sum of all the averages with the help of javascript functionalities. As we talk about average for subarrays we can use the reduce method of javascript.

What is the reduce() Method in JavaScript ?

Let's understand the working of a reduce function in JavaScript.

In javascript the reduce method is used to reduce an array to a single value by iterating over each and every item of the array. And by applying the callback function that accumulates a value based on the result of each iteration. The reduce method basically takes two arguments. An accumulator and a current value.

Accumulator is the accumulated value from the previous iteration or the initial value passed to reducer function. The current value is the current element being processed in the array.

Following is the syntax to define list in JavaScript : −

const arr = [1, 2, 3, 4, 5];
const sum = arr.reduce((acc, val) => acc + val, 0);
console.log(sum); 

Output

15

Logic for the Given Problem

In the given problem statement we will find the averages of each subarray and then return the sum of all the averages. For making this algorithm we will need to calculate the average for each subarray separately by iterating over the array and using the reduce method to get the sum of elements in each subarray, and then we will divide it by the length of the subarray.

Algorithm

Step 1 − Step 1: Declare a function named sumOfAverages taking in an subarray of elements as an input .

Step 2 − Step 1: Declare a function named sumOfAverages taking in an subarray of elements as an input .

Step 3 − A for loop must now be defined in order to go through every element of the subarray. Get the average of each element in the subarray using a reduce method inside the loop.

Step 4 − This step will calculate the sum of all the average values.

Step 5 − In the last step return the result as sum.

Example

// declare a function for getting averages
function sumOfAverages(arr) { 
  var sum = 0; 
  for (let subArr of arr) {
    var avg = subArr.reduce((acc, val) => acc + val) / subArr.length;
    sum += avg;
  }
  return sum;
}
const arr = [[1, 2, 3], [4, 5], [6, 7, 8, 9]];
const result = sumOfAverages(arr);
console.log(result);

Output

14

Complexity

Whenever we talk about time complexity, it is measured in terms of time taken to execute a particular function to get the result.

In our case, we have initially used a for loop inside the function that goes for length of the array. So the time taken by the for loop is O(n^2) time to iterate over each and every subarray in the array. It also uses the reduce function to get the sum of elements in each array. The reduce function itself takes O(n) time to execute. In this n is the length of the subarray.

If we talk about space complexity then this function will take O1) memory space because the function uses a constant amount of extra memory to store the sum and average values.

Conclusion

The most straightforward and dependable approach for calculating the average and sum of subarrays in JavaScript. As we have seen in the above implementation the function sumOfAverages calculates the average of each subarray separately and returns the sum of all the averages. It also has the time complexity of O(n^2) and space complexity O(1). This function may be suitable for small input arrays, but may not be efficient for large input arrays because of its high time complexity.

Updated on: 22-Aug-2023

132 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements