Given a&nbsp;square&nbsp;matrix&nbsp;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.

&nbsp;
Example 1:

Input: mat = [[1,2,3],
&nbsp;             [4,5,6],
&nbsp;             [7,8,9]]
Output: 25
Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
Notice that element mat[1][1] = 5 is counted only once.


Example 2:

Input: mat = [[1,1,1,1],
&nbsp;             [1,1,1,1],
&nbsp;             [1,1,1,1],
&nbsp;             [1,1,1,1]]
Output: 8


Example 3:

Input: mat = [[5]]
Output: 5


&nbsp;
Constraints:


	n == mat.length == mat[i].length
	1 &lt;= n &lt;= 100
	1 &lt;= mat[i][j] &lt;= 100


class Solution:
    def diagonalSum(self, mat: List[List[int]]) -> int:
        
        n = len(mat)
        
        mid = n // 2
        
        summation = 0
        
        for i in range(n):
            
            # primary diagonal
            summation += mat[i][i]
            
            # secondary diagonal
            summation += mat[n-1-i][i]
            
            
        if n % 2 == 1:
            # remove center element (repeated) on odd side-length case
            summation -= mat[mid][mid]
            
            
        return summation
class Solution {
    public int diagonalSum(int[][] mat) {
        int sum1 = 0;
        int sum2 = 0;
        int n = mat.length;
        for(int i = 0 ; i < n ; i++)
        {
            sum1 += mat[i][i];
            sum2 += mat[i][n-i-1];
        }
        int res = sum1 + sum2;
        if(n%2 != 0)
        {
            res -= mat[n/2][n/2];
        }
        return res;
    }
}
class Solution {
public:
    int diagonalSum(vector<vector<int>>& mat) {
     int n = mat.size() ;
     int ans = 0 ;

     for(int i = 0 ; i < n ; i++){
        ans = ans + mat[i][i] + mat[i][n - i - 1] ;
      }
      ans = (n & 1) ? ans - mat[n/2][n/2] : ans ;//if n is odd then we have to subtract mat[n/2][n/2] from the ans because we add it twice earlier.
      return ans ;
    }
};
var diagonalSum = function(mat) {
    return mat.reduce((acc, matrix, i)=>{
      const matrixlength = matrix.length-1;
       return acc += (i !== matrixlength-i) ? matrix[i]+ matrix[matrixlength-i] : matrix[i];
    },0)
};

/*
*/
Matrix Diagonal Sum