JavaScript Program to Find Difference Between Sums of Two Diagonals



To find difference between sums of two diagonals, we will be discussing two different approaches. We will calculate the sum of the elements present in the left and right diagonal, then we will subtract both sum values to get the difference.

In this article we are having a square matrix, our task is to write a JavaScript program to find difference between sums of two diagonals.

Matrix diagonal

Example

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

Sum of left diagonal elements = 1+5+9 = 15
Sum of right diagonal elements = 3+5+9 = 17

Output:
Difference: 2

Approaches to Find Diagonal Sums Difference

Here is a list of approaches to find difference between sums of two diagonals in Javascript which we will be discussing in this article with stepwise explaination and complete example codes.

Using Brute Force

To find difference between sums of two diagonals 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 diagonalDiff() that accepts matrix as argument.
  • Inside function diagonalDiff() 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).
  • The absolute difference between the two diagonal sums is calculated using the Math.abs() function and returned as the result.
  • The result is then displayed in web console using console.log().

Example

Here is a complete example code implementing above mentioned steps find difference between sums of two diagonals in Javascript using nested loop.

let matrix = [[1, 2, 3], [4, 5, 6], [9, 8, 9]];
function diagonalDiff(matrix) {
    let leftSum = 0;
    let rightSum = 0;
    let n = matrix.length;
    for (let i = 0; i < n; i++) {
        for(let j = 0; j < n; j++) {
            if (i === j) {
                leftSum += matrix[i][j];
            }
            if (i === n-1-j) {
                rightSum += matrix[i][j];
            }
        }
    }
    return Math.abs(leftSum - rightSum);
}

console.log("Difference of sum of elements of both diagonals: ")
console.log(diagonalDiff(matrix));

Optimized Approach

In this approach to find difference between sums of two diagonals 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 diagonalDiff() that accepts matrix as argument.
  • Inside function diagonalDiff() 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).
  • The absolute difference between the two diagonal sums is calculated using the Math.abs() function and returned as the result.
  • The result is then displayed in web console using console.log().

Example

Here is a complete example code implementing above mentioned steps find difference between sums of two diagonals in Javascript using for loop.

let matrix = [[1, 2, 3], [4, 5, 6], [9, 8, 9]];
function diagonalDiff(arr) {
    let leftSum = 0;
    let rightSum = 0;
    let n = arr.length;
    for (let i = 0; i < n; i++) {
        leftSum += arr[i][i];
        rightSum += arr[i][n - 1 - i];
    }
    return Math.abs(leftSum - rightSum);
}
console.log("Difference of sum of elements of both diagonals: ")
console.log(diagonalDiff(matrix));

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 find difference between sums of two diagonals 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-12T16:40:26+05:30

472 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements