🐥Very Simple Beginner Friendly Simulation Java/C Solution (O(N*M))🐥
Intuition\n Describe your first thoughts on how to solve this problem. \n# Update\nJust Figured Out ZeroCount is Not important as the minimum will handle it\n# 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n# Update\n**Just Figured Out ZeroCount is Not important as the minimum will handle it**\n# Approach\n![Screenshot 2024-11-24 at 1.47.12\u202FPM.png](https://assets.leetcode.com/users/images/684939bf-3b8a-421d-ad28-68d31d5a92aa_1732436244.38601.png)\n![Screenshot 2024-11-24 at 1.46.57\u202FPM.png](https://assets.leetcode.com/users/images/b52bcc08-af2b-4069-b21b-6614a6c652af_1732436272.7698033.png)\n\n<!-- Describe your approach to solving the problem. -->\n1. Since we can perform any number of operations ( multiplying adjacent pairs by -1) we can form a chain (irrespective of adjaceny)( shown in image )\n2. Also if a zero is found that means ( we can multiply that zero any number of times causing each negative to become possitive)\n3. so all we need to do is keep track of negative numbers and zero count\n4. if any zero is found that means all the negatives can turn into possitive\n5. if no zero is found and the number of negative elems is even then we can also change them to possitive\n6. but if number of negative elems is odd that means we can change all even pairs leaving only 1 negative ( that negative has to be the minimum |min| )\n# Complexity\n- Time complexity: O(N*M)\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 sum = 0;\n        int minVal = Integer.MAX_VALUE, negCount = 0, zeroCount = 0;\n        for (int[] row : matrix) {\n            for (int cell : row) {\n                if (cell < 0) {\n                    sum = sum - cell;\n                    negCount++;\n                    minVal = Math.min(minVal, -cell);\n                } else if (cell == 0) {\n                    zeroCount++;\n                    minVal = Math.min(minVal, 0);\n                } else {\n                    sum += cell;\n                    minVal = Math.min(minVal, cell);\n                }\n            }\n        }\n        if (zeroCount != 0 || negCount % 2 == 0)\n            return sum;\n        return sum - 2*minVal;\n    }\n}\n```\n```C []\nlong long maxMatrixSum(int** matrix, int matrixSize, int* matrixColSize) {\n    long long sum = 0;\n    int minVal = INT_MAX, negCount = 0, zeroCount = 0;\n    for (int i = 0; i < matrixSize; i++) {\n        for (int j = 0; j < matrixColSize[i]; j++) {\n            int cell = matrix[i][j];\n            if (cell < 0) {\n                sum -= cell;\n                negCount++;\n                if (-cell < minVal) minVal = -cell;\n            } else if (cell == 0) {\n                zeroCount++;\n                if (0 < minVal) minVal = 0;\n            } else {\n                sum += cell;\n                if (cell < minVal) minVal = cell;\n            }\n        }\n    }\n    return (zeroCount > 0 || negCount % 2 == 0) ? sum : sum - 2 * minVal;\n}\n```
['C', 'Matrix', 'Simulation', 'Java']