EASY FOR BEGINNERS! Python/JavaScript Brute Force Solution
Approach\nBrute Force Approach\n\n# Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space
# Approach\nBrute Force Approach\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Python Code\n```\nclass Solution:\n    def diagonalSum(self, mat: List[List[int]]) -> int:\n        n = len(mat)\n        total_sum = 0\n\n        for i in range(n):\n            total_sum += mat[i][i]  # Sum main diagonal element\n            total_sum += mat[i][n - 1 - i]  # Sum anti-diagonal element\n\n        # If the matrix has an odd dimension, subtract the middle element once\n        if n % 2 == 1:\n            total_sum -= mat[n // 2][n // 2]\n\n        return total_sum\n\n```\n# JavaScript Code\n```\nconst n = mat.length;\n    let total_sum = 0;\n\n    for (let i = 0; i < n; i++) {\n        total_sum += mat[i][i]; // Sum main diagonal element\n        total_sum += mat[i][n - 1 - i]; // Sum anti-diagonal element\n    }\n\n    // If the matrix has an odd dimension, subtract the middle element once\n    if (n % 2 === 1) {\n        total_sum -= mat[Math.floor(n / 2)][Math.floor(n / 2)];\n    }\n\n    return total_sum;\n```\n\n
['Python3']