Compute Sums of Diagonals of a Matrix in JavaScript



To efficiently compute sums of diagonals of a matrix, we will be discussing two different approaches. We will calculate the sum of the elements present in the left-to-right and right-to-left diagonal i.e primary and secondary diagonal respectively.

In this article we are having a square matrix, our task is to write a JavaScript program to efficiently compute sums of diagonals of a matrix.

Example

Input:
Matrix: [[1, 2, 3], [4, 5, 6], [9, 8, 9]]

Sum of primary diagonal elements = 1+5+9 
Sum of secondary diagonal elements = 3+5+9 

Output:
Sum of primary diagonal elements = 15
Sum of secondary diagonal elements = 17

Approaches to Find Diagonal Sums of Matrix

Here is a list of approaches to efficiently compute sums of diagonals of a matrix in Javascript which we will be discussing in this article with stepwise explaination and complete example codes.

Using Brute Force

To efficiently compute sums of diagonals of a matrix in Javascript, we have used nested for loop to iterate over each elements in rows and columns.

  • We have declared a 2D array matrix and defined a function diagonalSum() that accepts matrix as argument.
  • Inside function diagonalSum() we have declared two variables leftSum and rightSum to store the sum of left-to-right diagonal and right-to-left diagonal respectively.
  • We have used nested for loop to iterate through the matrix. To calculate the leftSum, value at the same position in both the row and the column is added with leftSum.
  • To calculate the rightSum, we have to add the elements whose index sum (i+J) is equal to (n-1) or i = (n-1-j).
  • After completing the loop leftSum and rightSum is displayed in web console using console.log() method. At the end function diagonalSum() is called.

Example

Here is a complete example code implementing above mentioned steps to efficiently compute sums of diagonals of a matrix in Javascript using nested loop.

let matrix = [[1, 2, 3], [4, 5, 6], [9, 8, 9]];
console.log(matrix);

function diagonalSum(matrix) {
    let leftSum = 0;
    let rightSum = 0;
    let n = matrix.length;
    for (let i = 0; i 


Optimized Approach

In this approach to efficiently compute sums of diagonals of a matrix in javascript we have used single for loop instead of using nested for loop as approach first.

  • We have declared a 2D array matrix and defined a function diagonalSum() that accepts matrix as argument.
  • Inside function diagonalSum() we have declared two variables leftSum and rightSum to store the sum of left-to-right diagonal and right-to-left diagonal respectively.
  • We have used a for loop to iterate over elements of the matrix. The leftSum is calculated by adding the leftSum with matrix elements having same index.
  • The rightSum is calculated by adding the elements whose index sum (i+J) is equal to (n-1) or i = (n-1-j).
  • After completing the loop leftSum and rightSum is displayed in web console using console.log() method. At the end function diagonalSum() is called.

Example

Here is a complete example code implementing above mentioned steps to efficiently compute sums of diagonals of a matrix in Javascript using for loop.

let matrix = [[1, 2, 3], [4, 5, 6], [9, 8, 9]];
console.log(matrix);

function diagonalSum(matrix) {
    let leftSum = 0;
    let rightSum = 0;
    let n = matrix.length;
    for (let i = 0; i 

Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Brute Force O(n^2) O(1)
Optimized Approach O(n) O(1)

Conclusion

In this article to write a JavaScript program to efficiently compute sums of diagonals of a matrix we have discussed two approaches. These are: by using brute force where we have used nested loop and optimized approach where we have used a single for loop. Using single for loop is more efficient approach as it has time complexity of O(n).

Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
Updated on: 2024-12-12T17:15:37+05:30

354 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements