class Solution:
def diagonalSum(self, mat):
n = len(mat)
total = 0
for i in range(n):
total += mat[i][i] # Primary diagonal
total += mat[i][n - 1 - i] # Secondary diagonal
if n % 2 == 1:
total -= mat[n // 2][n // 2] # Subtract the center if counted twice
return total
class Solution {
public:
int diagonalSum(vector<vector<int>>& mat) {
int n = mat.size();
int total = 0;
for (int i = 0; i < n; ++i) {
total += mat[i][i]; // Primary diagonal
total += mat[i][n - 1 - i]; // Secondary diagonal
}
if (n % 2 == 1) {
total -= mat[n / 2][n / 2]; // Subtract center if counted twice
}
return total;
}
};
class Solution {
public int diagonalSum(int[][] mat) {
int n = mat.length;
int total = 0;
for (int i = 0; i < n; i++) {
total += mat[i][i]; // Primary diagonal
total += mat[i][n - 1 - i]; // Secondary diagonal
}
if (n % 2 == 1) {
total -= mat[n / 2][n / 2]; // Subtract center if counted twice
}
return total;
}
}
/**
* @param {number[][]} mat
* @return {number}
*/
var diagonalSum = function(mat) {
const n = mat.length;
let total = 0;
for (let i = 0; i < n; i++) {
total += mat[i][i]; // Primary diagonal
total += mat[i][n - 1 - i]; // Secondary diagonal
}
if (n % 2 === 1) {
total -= mat[Math.floor(n / 2)][Math.floor(n / 2)]; // Subtract center if counted twice
}
return total;
};
You are given a square matrix mat
of size n x n
, where each element is an integer. Your task is to return the sum of the matrix's primary and secondary diagonals.
mat[i][i]
).mat[i][n-1-i]
).n
is odd, the center element belongs to both diagonals, so it should only be counted once in the sum.Constraints:
n == mat.length == mat[i].length
1 <= n <= 100
1 <= mat[i][j] <= 100
Let's break down the problem. We need to sum all the numbers that are on either of the two diagonals of the matrix. At first glance, a brute-force method would be to loop through every element and check if it belongs to either diagonal, but that's inefficient.
Instead, we notice that for a square matrix, the diagonal elements have predictable positions:
mat[i][i]
for all i
mat[i][n-1-i]
for all i
0
to n-1
, we can access and sum both diagonals directly.
However, if n
is odd, the center element (where i == n/2
) gets counted twice (once for each diagonal). We need to subtract it once to avoid double-counting.
Let's outline the efficient solution step by step:
mat[i][i]
(primary diagonal) to the total.mat[i][n-1-i]
(secondary diagonal) to the total.n
is odd):
n
is odd, the center element at mat[n//2][n//2]
appears in both diagonals. Subtract it once from the total.This approach is efficient because we only loop through the matrix once, and we avoid unnecessary checks or extra data structures.
Let's consider the example matrix:
mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
1
(mat[0][0]), 5
(mat[1][1]), 9
(mat[2][2])3
(mat[0][2]), 5
(mat[1][1]), 7
(mat[2][0])Step by step:
total = 0
Final answer: 25
The optimized solution is much faster and uses minimal memory, making it suitable even for the largest allowed matrices.
To solve the Matrix Diagonal Sum problem, we efficiently sum both diagonals by leveraging their predictable positions in the matrix. We loop through each row once, adding both diagonal elements, and adjust for a possible double-counted center if the matrix size is odd. This results in a clean, O(n) time and O(1) space solution that is both elegant and easy to implement.