Simple AF Java Solution
Intuition\nFind the relation between the row and column indices of both diagonals.\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n
# Intuition\nFind the relation between the row and column indices of both diagonals.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nPrimary Diagonal idices are equal (i==j) so the position will be mat[i][i];\nSecondary Diagonal indices sum upto n-1 so the position will be mat[i][n-i-1];\nSince we have one common point between primary diagonal and secondary diagonal we need put another condition:\nif(i!=n-i-1) sum+=mat[i][n-i-1];\nSo that we can avoid taking that position twice in the matrix diagonal sum.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n) because loop runs n times.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1) because we are not taking any new arrays or anything. Just one sum and one length variable.\n\n\n# Code\n```java []\nclass Solution {\n    public int diagonalSum(int[][] mat) {\n        int sum=0;\n        int n=mat[0].length;\n        for(int i=0;i<n;i++){\n            sum+=mat[i][i];\n            if(i!=n-i-1)\n                sum+=mat[i][n-i-1];\n        }\n        return sum;\n    }\n}\n```
['Java']