JavaScript Program to Find difference between sums of two diagonals


We will be finding the difference between the sums of two diagonals of a square matrix. Firstly, we will calculate the sum of the elements present in the first diagonal by traversing the matrix starting from the top left corner to the bottom right corner. Secondly, we will calculate the sum of the elements present in the second diagonal by traversing the matrix starting from the top right corner to the bottom left corner. Finally, we will subtract the sum of the second diagonal from the sum of the first diagonal to get the difference between the two diagonals.

Approach

  • To find the difference between the sums of two diagonals of a square matrix, the first step is to define a function that takes the matrix as an input.

  • Next, you need to loop through the matrix and calculate the sum of elements present in the first diagonal (top left to bottom right).

  • Similarly, calculate the sum of elements present in the second diagonal (top right to bottom left)

  • Subtract the second diagonal sum from the first diagonal sum and store the result in a variable.

  • Return the result variable, which would be the difference between the sums of two diagonals of the square matrix.

Example

Here is a JavaScript program to find the difference between the sums of two diagonals of a matrix −

function diagonalDifference(arr) {
   let leftToRightDiagonalSum = 0;
   let rightToLeftDiagonalSum = 0;
   let matrixSize = arr.length;
   for (let i = 0; i < matrixSize; i++) {
      leftToRightDiagonalSum += arr[i][i];
      rightToLeftDiagonalSum += arr[i][matrixSize - 1 - i];
   }
   return Math.abs(leftToRightDiagonalSum - rightToLeftDiagonalSum);
}
let matrix = [[1, 2, 3], [4, 5, 6], [9, 8, 9]];
console.log(diagonalDifference(matrix));

Explanation

  • The function diagonalDifference takes a 2-dimensional array (matrix) as an argument.

  • Two variables leftToRightDiagonalSum and rightToLeftDiagonalSum are declared to store the sum of left-to-right diagonal and right-to-left diagonal respectively.

  • The size of the matrix is stored in the matrixSize variable.

  • A for loop is used to iterate through the matrix. In each iteration, the current value in the matrix is added to both diagonals' sums.

  • To calculate the left-to-right diagonal sum, the value at the same position in both the row and the column is added to the leftToRightDiagonalSum.

  • To calculate the right-to-left diagonal sum, the value at the same position in the row as the column is subtracted from matrixSize - 1. This is because the right-to-left diagonal runs in the opposite direction to the left-to-right diagonal.

  • The absolute difference between the two diagonal sums is calculated using the Math.abs function and returned as the result.

  • A sample matrix is declared and passed to the diagonalDifference function, and the result is logged to the console.

The output of this program should be 2, which is the difference between the sums of the two diagonals of the sample matrix.

Updated on: 15-Mar-2023

253 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements