Simplest & Efficient C Code Time[O(n)], Space[O(1)]🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem asks to find the sum of the elements of the main diagonal and the secondary
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks to find the sum of the elements of the main diagonal and the secondary diagonal of a given square matrix. If the matrix has an odd dimension, we need to subtract the central element that belongs to both diagonals.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe function **diagonalSum** receives a pointer to a 2D array **mat**, an integer **matSize** indicating the size of the matrix and a pointer to an integer **matColSize** with the number of columns of each row. The approach to solving the problem is to iterate through each row of the matrix and add the elements of the main diagonal and the secondary diagonal to the variable **sum**. To find the elements of the main diagonal, we use the index **i** for both rows and columns of the matrix, i.e., **mat[i][i]**. To find the elements of the secondary diagonal, we use the index i for rows and matSize-i-1 for columns, **i**.e., **mat[i][matSize-i-1]**. Finally, if the matrix has an odd size, we need to subtract the central element, which belongs to both diagonals.\n\n# Complexity\n- Time complexity: $O(n)$, where n is the size of the matrix. We need to iterate through each row of the matrix once to find the sum of the diagonals.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $O(1)$, because we are only using a constant amount of extra space to store the sum of the diagonals and a few variables for iteration.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nint diagonalSum(int** mat, int matSize, int* matColSize){\n    int sum = 0;\n    for(int i=0;i<matSize;i++){\n        sum +=mat[i][i];\n        sum +=mat[i][matSize-i-1];\n    }\n    return matSize%2==0?sum:sum - mat[matSize/2][matSize/2];\n}\n```
['C']