Description

Given a square matrix mat, return the sum of the matrix diagonals. Only include the sum of all elements on the primary and secondary diagonals that are not part of both diagonals.

Examples

Input:mat = [[1,2,3],[4,5,6],[7,8,9]]
Output:25
Explanation:

1+5+9+3+7 = 25 (middle counted once).

Input:mat = [[1]]
Output:1
Explanation:

A 1x1 matrix has one element on both diagonals. The sum is 1.

Input:mat = [[5,1,3,8],[2,7,4,6],[9,3,2,1],[4,8,7,5]]
Output:38
Explanation:

Primary diagonal: 5 + 7 + 2 + 5 = 19. Secondary diagonal: 8 + 4 + 3 + 4 = 19. Since the matrix is 4x4 (even), no element is on both diagonals. Total = 19 + 19 = 38.

Constraints

  • n == mat.length == mat[i].length
  • 1 ≤ n ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!