Easy java solution | Beats 100% | O(n) Time complexity
Intuition\n Describe your first thoughts on how to solve this problem. \nFirstly, we are given a sqaure matrix so we do not need seperate pointers for row and c
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirstly, we are given a sqaure matrix so we do not need seperate pointers for row and column.\nWe need to find sum of diagonals of the square matrix for which we will need to deduce a relation between row and column for both the primary and the secondary diagonal.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach is pretty simple, we keep a pointer for first and last row of the matrix and find the sum of diagonal elements, we move to second and second last row. We keep on doing `n/2` times where `n` is the size of square matrix.\nNow in the end, if the matrix length `n` is odd we add the center most element to the sum and return it.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n# Code\n```\nclass Solution {\n    public int diagonalSum(int[][] mat) {\n        int matLen = mat.length;\n        int ans = 0;\n        for(int i=0; i<matLen/2; i++){\n            ans += mat[i][i] + mat[i][matLen-1-i] + mat[matLen-1-i][i] + mat[matLen-1-i][matLen-1-i]; \n            //Addition of all the diagonal(primary and secondary diagonal) elements for ith row and ith row from the bottom of the matrix\n        }        \n        if(matLen%2 != 0)\n            ans += mat[matLen/2][matLen/2];        \n            //Adding the center most element if the matrix length is odd\n        return ans;\n    }\n}\n```
['Java']