C# Solution for Maximum Matrix Sum Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires maximizing the sum of the matrix elements by flipping signs of adj
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires maximizing the sum of the matrix elements by flipping signs of adjacent elements. The key insights in this solution are:\n1.\tFlipping Negatives to Maximize Sum:\n\t\u2022\tIf all elements are positive, the sum is simply the sum of the matrix.\n\t\u2022\tIf negatives exist, we aim to flip as many as possible to positive while minimizing the loss caused by unavoidable negatives.\n2.\tParity of Negatives:\n\t\u2022\tIf there are an even number of negative elements, all can be flipped to positive.\n\t\u2022\tIf there are an odd number of negatives, one element must remain negative, and we choose the smallest absolute value to minimize the penalty.\n3.\tSingle Pass Calculations:\n\t\u2022\tTraverse the matrix once, calculate the absolute sum of elements, track the smallest absolute value, and determine the parity of negatives\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tMatrix Traversal:\n\t\u2022\tFor each element:\n\t\u2022\tAdd its absolute value to the totalSum.\n\t\u2022\tTrack the smallest absolute value (minAbsValue) to determine the least costly element to leave negative.\n\t\u2022\tToggle the parity tracker (hasOddNegatives) if the element is negative.\n2.\tFinal Adjustment:\n\t\u2022\tAfter traversing the matrix:\n\t\u2022\tIf hasOddNegatives is true (odd negatives), subtract twice the minAbsValue from totalSum to account for the smallest unavoidable negative.\n3.\tReturn Result:\n\t\u2022\tReturn the adjusted totalSum.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nMatrix Traversal: The matrix is traversed once, processing each element in O(1) time.\n\u2022\tTime Complexity: O(n^2), where n is the number of rows (and columns) in the matrix.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nExtra Variables: Only a few variables are used:\n\t\u2022\ttotalSum to store the absolute sum of elements.\n\t\u2022\tminAbsValue to track the smallest absolute value.\n\t\u2022\thasOddNegatives to track the parity of negatives.\n\u2022\tSpace Complexity: O(1).\n\n# Code\n```csharp []\npublic class Solution {\n    public long MaxMatrixSum(int[][] matrix) {\n        long totalSum = 0; \n        int minAbsValue = int.MaxValue; \n        bool hasOddNegatives = false; \n\n        int n = matrix.Length;\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < n; j++) {\n                int value = matrix[i][j];\n                int absValue = Math.Abs(value);\n                totalSum += absValue;\n                if (absValue < minAbsValue) {\n                    minAbsValue = absValue;\n                }\n                if (value < 0) {\n                    hasOddNegatives = !hasOddNegatives;\n                }\n            }\n        }\n        if (hasOddNegatives) {\n            totalSum -= 2 * minAbsValue;\n        }\n\n        return totalSum;\n    }\n}\n```
['C#']