Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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 are two approaches to find difference between sums of two diagonals in JavaScript:
Using Brute Force
In this approach, we use nested for loops to iterate over each element in the matrix and check if it belongs to either diagonal.
- Create a function that accepts the matrix as a parameter
- Initialize variables to store sums of both diagonals
- Use nested loops to traverse the matrix
- For main diagonal: check if row index equals column index (i === j)
- For anti diagonal: check if i === n-1-j
- Return the absolute difference using Math.abs()
Example
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("Matrix:");
console.log(matrix);
console.log("Difference of sum of elements of both diagonals:");
console.log(diagonalDiff(matrix));
Matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 9, 8, 9 ] ] Difference of sum of elements of both diagonals: 2
Optimized Approach
This approach uses a single for loop to calculate both diagonal sums simultaneously, making it more efficient.
- Use a single loop from 0 to n-1
- Access main diagonal element at matrix[i][i]
- Access anti diagonal element at matrix[i][n-1-i]
- Calculate both sums in the same iteration
- Return the absolute difference
Example
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]; // Main diagonal
rightSum += arr[i][n - 1 - i]; // Anti diagonal
}
return Math.abs(leftSum - rightSum);
}
console.log("Matrix:");
console.log(matrix);
console.log("Difference of sum of elements of both diagonals:");
console.log(diagonalDiff(matrix));
Matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 9, 8, 9 ] ] Difference of sum of elements of both diagonals: 2
Complexity Comparison
Here is a comparison of time and space complexity of both approaches:
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Brute Force | O(n²) | O(1) |
| Optimized Approach | O(n) | O(1) |
Conclusion
The optimized approach with a single loop is more efficient with O(n) time complexity compared to the brute force O(n²) approach. Both methods provide the same result but the optimized version is preferred for larger matrices.
