Well Explained Code|| Beats 100% ||
\n\n# Approach\nThe solution uses a for loop to iterate through the matrix and add the elements present in the main diagonal and the secondary diagonal.\n\nThe 
\n\n# Approach\nThe solution uses a for loop to iterate through the matrix and add the elements present in the main diagonal and the secondary diagonal.\n\nThe main diagonal of a 2D square matrix is the diagonal that runs from the top left corner to the bottom right corner. The elements present in the main diagonal are those where the row index and the column index are the same. In other words, the element present at (i, i) is present in the main diagonal, where i ranges from 0 to n-1, where n is the number of rows (or columns) in the matrix.\n\nSimilarly, the secondary diagonal of a 2D square matrix is the diagonal that runs from the top right corner to the bottom left corner. The elements present in the secondary diagonal are those where the row index and the column index are different. In other words, the element present at (i, n-1-i) is present in the secondary diagonal, where i ranges from 0 to n-1, and n is the number of rows (or columns) in the matrix (property of matrix).\n\nIn the given solution, the for loop starts from the first row and continues till the last row. For each iteration, the element present at (i, i) in the main diagonal is added to the sum. If the current row is not equal to the difference of the length of the matrix and the current row, then the element present at (i, n-1-i) in the secondary diagonal is also added to the sum. This check is necessary because if the matrix is an odd-sized matrix, then the middle element would be present in both the diagonals, and adding it twice would result in incorrect output.\n\nFinally, the sum is returned as the result.\n\nThis approach ensures that the elements present in both the diagonals are added only once, and the result obtained is the sum of the elements present in the main diagonal and the secondary diagonal.\n\n\n\n\n\n\n# Complexity\n- Time complexity:\nThe time complexity of this solution is O(n), where n is the number of rows (or columns) in the matrix. The for loop runs n times, and each iteration takes constant time to add the elements present in the main and secondary diagonals. Hence, the time complexity is linear.\n\n- Space complexity:\nThe space complexity of this solution is O(1), as it only uses a single variable sum to store the result. No additional data structures are used, and the memory usage remains constant regardless of the size of the input matrix.\n\n# Code\n```\nclass Solution {\n    public int diagonalSum(int[][] mat) {\n        int sum =0;\n        for(int i=0;i<mat.length;i++){\n            sum += mat[i][i];\n            if(i != mat.length -1 -i){ \n                sum += mat[i][mat.length -1 -i];\n            }\n        }\n        return sum;\n    }\n}\n```\n![upvote.jpeg](https://assets.leetcode.com/users/images/365d5b2e-d6e9-40ac-91e4-202d80e04c24_1675533542.172329.jpeg)\n\n
['Array', 'Matrix', 'Java']