0ms ✅|| O(n)😍 || Easy Approach 🔥|| Beasts 100%👌
Intuition\n Describe your first thoughts on how to solve this problem. \nWe will simply add the primary and secondar diagonal values by using one pointer approa
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe will simply add the primary and secondar diagonal values by using one pointer approach (by taking `i and (n-1)-i` values) and at last subtract the reapeated value from the sum if the matrix size is odd.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nAt first simply iterating the loop from `0 --> N-1` for i.\n    - **Primary Diagonal** - `mat[i][i]` for every (0 ,0) , (1 ,1)........  till (n-1 , n-1). \n    - **Secondary Diagonal** - `mat[i][n-1-i]` for every (0 ,n-1) , (1 , n-2) .....till (n-1, 0).\n    - After that, if **n** (`mat.length`) is odd then we will substract the middle value from the matrix as it is repeated twice. i.e. `mat[n/2][n/2]` . \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![image.png](https://assets.leetcode.com/users/images/b542fb55-082f-415a-bf34-c31edf48177d_1683558353.529726.png)\n\n### Please - Upvote :) \uD83D\uDD25\n# Code\n```\nclass Solution {\n  public int diagonalSum(int[][] mat) {\n    \n    int sum = 0, n = mat.length;\n\n    for (var i=0; i<n; i++){ \n        sum += mat[i][i] + mat[i][n-1-i];\n    }\n    if (n%2 == 1){ //Removing the middle element n is odd as it is repeated.\n        sum -= mat[n/2][n/2];\n    }\n    return sum;\n  }\n}\n```
['Matrix', 'Java']