Odd/Even "-ve" check | Solution for LeetCode#1975
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this problem is to realize that we can make all numbers posi
![image.png](https://assets.leetcode.com/users/images/59911dd0-2742-4285-85dd-b2249ffd5e81_1732409477.8745577.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this problem is to realize that we can make all numbers positive except possibly one, depending on the number of negative values in the matrix.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Iterate through the matrix and do the following:\n\t- Keep track of the sum of absolute values of all elements.\n\t- Count the number of negative values.\n\t- Keep track of the minimum absolute value.\n2. After processing all elements:\n\t- If the count of negative values is even, we can make all numbers positive, so return the sum of absolute values.\n\t- If the count of negative values is odd, we need to keep one number negative. To maximize the sum, we keep the smallest absolute value negative. So we subtract twice this value from the sum of absolute values.\n\t\n# Complexity\n- Time complexity: O(m * 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```java []\nclass Solution {\n    public long maxMatrixSum(int[][] matrix) {\n        long maxSum=0;\n        int negCnt=0;\n        int minNegVal=Integer.MAX_VALUE;\n\n        int m=matrix.length;\n        int n=matrix[0].length;\n\n        for(int i=0; i<m; i++){\n            for(int j=0; j<n; j++){\n                int val=Math.abs(matrix[i][j]);\n                minNegVal=Math.min(minNegVal, val);\n                maxSum+=val;                \n                if(matrix[i][j]<0){// Negative                    \n                    negCnt++;                    \n                }                \n            }\n        }\n\n\n        if(negCnt%2==0){\n            return maxSum;\n        }\n        else{\n            return maxSum-2*minNegVal;\n        }\n       \n    }\n}\n```
['Java']