[ C++ ] | Easy Explanation | 2 Pointers
\n\n# Approach\n Describe your approach to solving the problem. \n2 Pointer Approach i and n-i-1 to get first and last column, then just normal Row Traversal\n\
\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n2 Pointer Approach `i` and `n-i-1` to get first and last column, then just normal `Row` Traversal\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 sum = 0;\n        int n = mat.size(); // get the size of the matrix\n\n        // loop through the rows of the matrix\n        for(int i = 0; i < n; i++){\n            // check if the current row is not on the main diagonal or the secondary diagonal\n            if(i != (n-i-1)){\n                // add the values at the two diagonal positions to the sum\n                sum += mat[i][i] + mat[i][n-i-1];\n            }\n            else{\n                // add the value at the middle position of the main diagonal to the sum\n                sum += mat[i][n-i-1];\n            }\n        }\n\n        // return the sum of the diagonal values\n        return sum;\n    }\n};\n\n```
['Array', 'Two Pointers', 'Matrix', 'C++']