🚩Easy C++ O(N) Soln. [ FAANG😱 Interview Optimized code ]
\n\n# Approach\n1) Add all elements at mat[i][i] & mat[i][n-i-1].\n2) If n is odd, then subtract the common element i.e mat[n/2][n/2]\n Describe your approach t
\n\n# Approach\n1) Add all elements at mat[i][i] & mat[i][n-i-1].\n2) If n is odd, then subtract the common element i.e mat[n/2][n/2]\n<!-- Describe your approach to solving the problem. -->\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# Code\n```\nclass Solution {\npublic:\n    int diagonalSum(vector<vector<int>>& mat) {\n        int n=mat.size();\n        int sum = 0;\n        for(int i=0;i<n;i++){\n            sum+=mat[i][i]+mat[i][n-i-1];\n        }\n        if(n%2)\n            sum-=mat[n/2][n/2];\n        \n        return sum;\n    }\n    \n};\n```
['Array', 'Matrix', 'C++', 'Java', 'Python3']