Matrix Diagonal Sum (LeetCode 1572): Solution and Explanation

Easy
Array
Matrix

Given a square matrix, add up the values on both diagonals. A cell that sits on both diagonals, which only happens at the centre of an odd-sized matrix, is counted once.

Intuition

You do not need to scan the whole matrix. Each row contains exactly one cell of the primary diagonal and exactly one cell of the secondary diagonal, so the work is one loop over the rows.

In row i the primary cell is in column i, and the secondary cell is in column n - 1 - i because that diagonal runs from the right edge back to the left.

The two columns collide only when i equals n - 1 - i, which is the middle row of an odd-sized matrix. There the same cell would be added twice, so you add it once and skip the second add.

Approach: One pass over the rows

  1. Set total = 0 and n = len(mat).
  2. For each row i, add mat[i][i] to total.
  3. If i != n - 1 - i, also add mat[i][n - 1 - i].
  4. Return total after the last row.

Step-by-step example

mat = [[1,2,3],[4,5,6],[7,8,9]]
  1. total = 0.
  2. Row 0: add mat[0][0] = 1 and mat[0][2] = 3, total = 4.
  3. Row 1: add mat[1][1] = 5, total = 9. Column n-1-1 is also 1, so the centre is not added again.
  4. Row 2: add mat[2][2] = 9 and mat[2][0] = 7, total = 25.
  5. Return 25.

Code: Python, Java, C++

1class Solution:
2    def diagonalSum(self, mat: List[List[int]]) -> int:
3        n = len(mat)
4        total = 0
5        for i in range(n):
6            total += mat[i][i]
7            if i != n - 1 - i:
8                total += mat[i][n - 1 - i]
9        return total
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One loop over n rows with at most two additions each.

Space

O(1)

Only the running total and the loop index are stored.

Other approaches

Add both, then subtract the centre (time O(n), space O(1))

Always add both diagonal cells in every row, then subtract mat[n/2][n/2] once at the end if n is odd. Same cost, and some people find it easier to remember.

Scan every cell (time O(n^2), space O(1))

Loop over all cells and add the ones where i == j or i + j == n - 1. The or handles the centre naturally, but it reads n squared cells instead of 2n.

Common mistakes

  • Counting the centre cell twice when n is odd.
  • Subtracting the centre when n is even, where there is no shared cell.
  • Using column n - i instead of n - 1 - i for the secondary diagonal, which reads past the end of the row.

Frequently asked questions

When do the two diagonals share a cell?

Only when n is odd. The shared cell is the exact centre, mat[n/2][n/2], where i equals n - 1 - i.

Why is the secondary column n - 1 - i?

That diagonal starts at the top-right corner, column n - 1, and moves one column left for every row down, so in row i it is at n - 1 - i.

What is the time complexity of Matrix Diagonal Sum?

O(n), because each of the n rows contributes at most two cells. No extra memory is needed.