Calculate the difference between the first and second element of each subarray separately and return the sum of their differences in JavaScript


In the given problem statement we are asked to calculate the difference between the first and second element of each subarray separately and we have to return the sum of their differences with the help of javascript functionalities. In the array data structures we can define an array which can also contain subarrays.

What is an Array of Subarrays in JavaScript ?

Let's understand the working of a list in JavaScript.

In javascript we can define array of subarray or nested array. An array of subarrays is an array which contains one or more arrays as its elements. Every array item within the main array is a subarray and this can also be called a nested array. So let's understand with example:

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

In the above example, an array is an array of subarrays. Each subarray contains 2 elements.

We can also access individual items of a subarray by using the index of the subarray and the index of the element within the subarray. Example: to access the value 4 in the second subarray, we can use the below code:

console.log(array[1][1]);

Output

4

We can also use array methods like map, reduce, forEach and many others to iterate through elements of subarrays. Arrays of subarrays are basically used in programming to represent and manipulate two-dimensional data, like matrices and tables.

Logic for The Above Problem

The easiest way to calculate the difference of elements of subarrays in javascript is by using reduce method and indexing of elements.

So let's understand the logic of the problem given. To calculate the difference between the first and second elements of a subarray we will declare a variable to store the sum of the difference. After that we will iterate through every subarray using a for loop. Then we will calculate the difference between its second and first element using the index of each item. Finally we will add the difference of each subarray using arithmetic operators.

Algorithm

Step 1 − Declare an array named arr containing subarrays of integer elements.

Step 2 − Declare the final sum of averages of subarrays.

Step 3 − This step will calculate the difference between the second and first element of each subarray by taking their index values.

Step 4 − Now forwarding to the third step, add all the average values of subarrays in this step.

Step 5 − Now show the output as the sum of averages of subarrays.

Example for Algorithm

// define array of subarrays 
const arr = [[1, 3], [7, 9], [11, 5]]; 

//calculate difference and their sum
const sum = arr.reduce((acc, subArr) => {  
  const diff = subArr[1] - subArr[0];  
  return acc + diff; 
}, 0);

console.log("The sum of the differences of subarrays: ");
console.log(sum);

Example using a for loop

// define array of subarrays 
const arr = [[1, 3], [7, 9], [11, 5]];
let sum = 0;

//calculate difference and their sum
for (let i = 0; i < arr.length; i++) {
  const subArr = arr[i];
  const diff = subArr[1] - subArr[0];
  sum += diff;
}
console.log("The sum of the differences of subarrays: ");
console.log(sum);

Output

The sum of the differences of subarrays:
-2

Time Complexity

The code we have implemented has an O(n) time complexity, in this n denotes the number of subarrays in the input array arr. The reason for this time complexity is that we are iterating through each subarray only once and executing the same work on each subarray. And we use an array arr to hold the input and fixed amount of memory for the sum of averages and the intermediate variables within the reduce() callback function So the overall space complexity will also be O(n).

Conclusion

Finally in this code the total of differences between the first and second items of each subarray in an array of subarrays is calculated using the reduce method. It is an efficient and easy way to solve this problem in Javascript. And in terms of time and space complexity the amount is O(n).

Updated on: 22-Aug-2023

213 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements