Java easy solution | Double pointer |efficient solution
Intuition\n Describe your first thoughts on how to solve this problem. \nProblem requires us to find the sum of diagonal elements of a square matrix. To do so, 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nProblem requires us to find the sum of diagonal elements of a square matrix. To do so, we need to traverse the diagonals of the matrix and sum up the corresponding elements. We can do this by using two pointers, one starting from the top-left corner and moving towards the bottom-right corner, and the other starting from the top-right corner and moving towards the bottom-left corner.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Initializes three variables i, j, and k to keep track of the row index, left column index, and right column index respectively. It also initializes a variable s to keep track of the sum of diagonal elements. The while loop runs till i is less than the number of rows in the matrix.\n\n- For each iteration of the while loop, the code checks if k is not equal to j, then it adds the values at mat[i][k] and mat[i][j] to the sum s. This is because, for each row, we are considering two elements on the diagonal, except for the middle row in case of an odd-sized matrix, where we only add the middle element once. If k is equal to j, then we add only the element at mat[i][j] to the sum s. Finally, the loop increments i, k, and decrements j.\n\n- At the end of the while loop, the sum s is returned as the sum of diagonal elements of the matrix.\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 {\n    public int diagonalSum(int[][] mat) {\n      int i=0,j=mat.length-1,k=0,s=0;\n      while(i<mat.length){\n           if(k!=j)\n           s+=(mat[i][k]+mat[i][j]);\n           else\n           s+=mat[i][j];\n           i++;k++;j--;\n      }\n      return s;\n    }\n}\n```
['Java']