Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

1572. Matrix Diagonal Sum 🌟

Given a square matrix mat, return the sum of the matrix diagonals.

O(N) Time O(1) Space solution

Code

class Solution{
public:
    int diagonalSum(vector<vector<int>> &mat)
    {
        int n = mat.size(), ans = 0;
        for (int i = 0; i < n; i++)
            ans += mat[i][i], ans += mat[i][n - 1 - i];
        if (n & 1)
            ans -= mat[n / 2][n / 2];
        return ans;
    }
};