Matrix Diagonal Sum Solution
In this page, we will solve the Matrix Diagonal Sum problem using multiple approaches. We will provide the implementation of the solution in Python, JavaScript, TypeScript, Java, and C++.
Problem Descriptionβ
Given a square matrix mat
, return the sum of the matrix diagonals.
Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.
Examplesβ
Example 1:
Input: mat = [[1,2,3],
[4,5,6],
[7,8,9]]
Output: 25
Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
Example 2:
Input: mat = [[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
[1,1,1,1]]
Output: 8
Explanation: Diagonals sum: 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 8
Constraintsβ
Solution for Matrix Diagonal Sum Problemβ
Intuition and Approachβ
We can solve this problem by iterating over the matrix and summing up the elements on both diagonals.
- Iterative Approach
- Optimized Approach
Approach: Iterativeβ
In this approach, we iterate through the matrix and add elements from the primary diagonal and the secondary diagonal.
Implementationβ
function matrixDiagonalSum() { const mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; const diagonalSum = function(mat) { let sum = 0; const n = mat.length; for (let i = 0; i < n; i++) { sum += mat[i][i]; // Primary diagonal if (i !== n - 1 - i) { // Exclude middle element for odd-sized matrix sum += mat[i][n - 1 - i]; // Secondary diagonal } } return sum; }; const result = diagonalSum(mat); return ( <div> <p> <b>Input:</b> mat = {JSON.stringify(mat)} </p> <p> <b>Output:</b> {result} </p> </div> ); }
Codes in Different Languagesβ
- JavaScript
- TypeScript
- Python
- Java
- C++
function diagonalSum(mat) {
let sum = 0;
const n = mat.length;
for (let i = 0; i < n; i++) {
sum += mat[i][i]; // Primary diagonal
if (i !== n - 1 - i) { // Exclude middle element for odd-sized matrix
sum += mat[i][n - 1 - i]; // Secondary diagonal
}
}
return sum;
}
function diagonalSum(mat: number[][]): number {
let sum = 0;
const n = mat.length;
for (let i = 0; i < n; i++) {
sum += mat[i][i]; // Primary diagonal
if (i !== n - 1 - i) { // Exclude middle element for odd-sized matrix
sum += mat[i][n - 1 - i]; // Secondary diagonal
}
}
return sum;
}
class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
diagonal_sum = 0
n = len(mat)
for i in range(n):
diagonal_sum += mat[i][i] # Primary diagonal
if i != n - 1 - i: # Exclude middle element for odd-sized matrix
diagonal_sum += mat[i][n - 1 - i] # Secondary diagonal
return diagonal_sum
class Solution {
public int diagonalSum(int[][] mat) {
int sum = 0;
int n = mat.length;
for (int i = 0; i < n; i++) {
sum += mat[i][i]; // Primary diagonal
if (i != n - 1 - i) { // Exclude middle element for odd-sized matrix
sum += mat[i][n - 1 - i]; // Secondary diagonal
}
}
return sum;
}
}
class Solution {
public:
int diagonalSum(vector<vector<int>>& mat) {
int sum = 0;
int n = mat.size();
for (int i = 0; i < n; i++) {
sum += mat[i][i]; // Primary diagonal
if (i != n - 1 - i) { // Exclude middle element for odd-sized matrix
sum += mat[i][n - 1 - i]; // Secondary diagonal
}
}
return sum;
}
};
Complexity Analysisβ
- Time Complexity:
- Space Complexity:
- Where
n
is the size of the square matrixmat
. - The time complexity is linear because we iterate through the matrix only once.
- The space complexity is constant as we use only a few variables irrespective of the size of the input.
Approach: Optimizedβ
In this approach, we optimize the previous solution by iterating only once through the matrix. We use two pointers to traverse both diagonals simultaneously.
Implementationβ
function matrixDiagonalSum() { const mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; const diagonalSum = function(mat) { let sum = 0; const n = mat.length; for (let i = 0, j = n - 1; i < n; i++, j--) { sum += mat[i][i]; // Primary diagonal if (i !== j) { // Exclude middle element for odd-sized matrix sum += mat[i][j]; // Secondary diagonal } } return sum; }; const result = diagonalSum(mat); return ( <div> <p> <b>Input:</b> mat = {JSON.stringify(mat)} </p> <p> <b>Output:</b> {result} </p> </div> ); }
Codes in Different Languagesβ
- JavaScript
- TypeScript
- Python
- Java
- C++
function diagonalSum(mat) {
let sum = 0;
const n = mat.length;
for (let i = 0, j = n - 1; i < n; i++, j--) {
sum += mat[i][i]; // Primary diagonal
if (i !== j) { // Exclude middle element for odd-sized matrix
sum += mat[i][j]; // Secondary diagonal
}
}
return sum;
}
function diagonalSum(mat: number[][]): number {
let sum = 0;
const n = mat.length;
for (let i = 0, j = n - 1; i < n; i++, j--) {
sum += mat[i][i]; // Primary diagonal
if (i !== j) { // Exclude middle element for odd-sized matrix
sum += mat[i][j]; // Secondary diagonal
}
}
return sum;
}
class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
diagonal_sum = 0
n = len(mat)
for i in range(n):
diagonal_sum += mat[i][i] # Primary diagonal
if i != n - 1 - i: # Exclude middle element for odd-sized matrix
diagonal_sum += mat[i][n - 1 - i] # Secondary diagonal
return diagonal_sum
class Solution {
public int diagonalSum(int[][] mat) {
int sum = 0;
int n = mat.length;
for (int i = 0, j = n - 1; i < n; i++, j--) {
sum += mat[i][i]; // Primary diagonal
if (i != j) { // Exclude middle element for odd-sized matrix
sum += mat[i][j]; // Secondary diagonal
}
}
return sum;
}
}
class Solution {
public:
int diagonalSum(vector<vector<int>>& mat) {
int sum = 0;
int n = mat.size();
for (int i = 0, j = n - 1; i < n; i++, j--) {
sum += mat[i][i]; // Primary diagonal
if (i != j) { // Exclude middle element for odd-sized matrix
sum += mat[i][j]; // Secondary diagonal
}
}
return sum;
}
};
Complexity Analysisβ
- Time Complexity:
- Space Complexity:
- Where
n
is the size of the square matrixmat
. - The time complexity is linear because we iterate through the matrix only once.
- The space complexity is constant as we use only a few variables irrespective of the size of the input.
By using these approaches, we can efficiently solve the Matrix Diagonal Sum problem for the given constraints.
Referencesβ
- LeetCode Problem: LeetCode Problem
- Solution Link: Matrix Diagonal Sum Solution on LeetCode
- Authors LeetCode Profile: Manish Kumar Gupta