Clean Python |  High Speed | Lazy Diagonal Sum + Repeated Element Check
Clean Python |  High Speed | Lazy Diagonal Sum + Repeated Element Check\n\nClean Python solution showcasing a One-Pass algorithm to perform a sum over both diag
**Clean Python |  High Speed | Lazy Diagonal Sum + Repeated Element Check**\n\nClean Python solution showcasing a One-Pass algorithm to perform a sum over both diagonals. At the end of the process, we check if the square matrix has an "odd" size. If this happened, we need to subtract the middle element which was summed twice. Otherwise, everything is fine (it\'s easy to prove/visualize that for matrices of even size the diagonals don\'t intersect, so nothing gets repeated while summing).\n\n* **Time Complexity:** O(N), where "N" is the matrix size.\n* **Space Complexity:** O(1), since we only use scalar variables.\n* **Edge Case Handling:** O(1) Time/Space Complexity (We correct our results only once. The O(n) loop isn\'t affected at all).\n\nI hope the explanation was helpful.\nCheers,\n\n\n\n```\n# Python\nclass Solution:\n    def diagonalSum(self, A):\n        n   = len(A)\n        res = 0\n        for i in range(n):\n            res += A[i][i] + A[i][n-1-i]\n        # x: Index of Element Repeated (when n is Odd)\n        x = (n-1)//2\n        return res - A[x][x] if n&1 else res\n```
['Python', 'Python3']