🧑‍💻Best Java Approach || Matrix Math || ⚡Runtime 4 ms
Intuition\nTo maximize the sum of the elements in the matrix by flipping adjacent elements, we need to understand the effect of these operations on the overall 
# Intuition\nTo maximize the sum of the elements in the matrix by flipping adjacent elements, we need to understand the effect of these operations on the overall sum. The key observation is that flipping pairs of elements can either increase or decrease the sum depending on their signs and magnitudes. If the number of negative elements is even, we can flip all pairs to positive, maximizing the sum. If the number of negative elements is odd, we will be left with one negative element, and we should minimize its absolute value to maximize the sum.\n\n# Approach\n1. **Traverse the Matrix**: Iterate through each element of the matrix to calculate the total sum of absolute values and find the minimum absolute value.\n2. **Count Negative Elements**: Use a boolean flag to track if the number of negative elements is odd.\n3. **Calculate Absolute Sum**: For each element, if it is negative, toggle the flag and convert the value to its positive counterpart. Add this positive value to the total sum.\n4. **Track Minimum Absolute Value**: Update the minimum absolute value if the current absolute value is smaller.\n5. **Adjust for Odd Negatives**: If the number of negative elements is odd, subtract twice the minimum absolute value from the total sum to account for the one remaining negative element.\n\n# Complexity\n- **Time complexity**: $$O(n^2)$$\n  - We traverse the matrix once, which involves iterating through each element in the matrix.\n- **Space complexity**: $$O(1)$$\n  - We use a constant amount of extra space regardless of the input size.\n\n\n# Code\n```java []\nclass Solution {\n    public long maxMatrixSum(int[][] matrix) {\n        long totalSum = 0;\n        int rows = matrix.length;\n        int cols = matrix[0].length;\n        int minAbsValue = Integer.MAX_VALUE;\n        boolean hasOddNegatives = false;\n\n        // Traverse the matrix to calculate the total sum of absolute values\n        // and find the minimum absolute value\n        for (int i = 0; i < rows; i++) {\n            for (int j = 0; j < cols; j++) {\n                int value = matrix[i][j];\n                if (value < 0) {\n                    hasOddNegatives = !hasOddNegatives; // Toggle the flag for odd negatives\n                    value = -value; // Convert negative to positive for sum calculation\n                }\n                totalSum += value; // Add the absolute value to the total sum\n                if (value < minAbsValue) {\n                    minAbsValue = value; // Update the minimum absolute value\n                }\n            }\n        }\n\n        // If there is an odd number of negative values, adjust the total sum\n        // by subtracting twice the minimum absolute value\n        if (hasOddNegatives) {\n            totalSum -= minAbsValue * 2;\n        }\n\n        return totalSum;\n    }\n}\n```\n\n# \u2B06\uFE0FKindly, Upvote\n
['Math', 'Matrix', 'Java']