Simple and Easy Solution | ✅Beats 100% || C++| Java | Python | JavaScript
\n---\n# \u2B06\uFE0FUpvote if it helps \u2B06\uFE0F\n\n## Connect with me on Linkedin Bijoy Sing.\n\n###### Solution in C++, Python, Java, and JavaScript\n\ncp
\n---\n# \u2B06\uFE0FUpvote if it helps \u2B06\uFE0F\n\n## Connect with me on Linkedin [Bijoy Sing](https://www.linkedin.com/in/bijoy-sing-236a5a1b2/).\n\n###### *Solution in C++, Python, Java, and JavaScript*\n\n```cpp []\nclass Solution {\npublic:\n    long long maxMatrixSum(vector<vector<int>>& matrix) {\n        int neg = 0;\n        long long minVal = LLONG_MAX;\n        long long sum = 0;\n\n        for (const auto& vec : matrix) {\n            for (const auto& val : vec) {\n                if (val < 0) {\n                    neg++;\n                }\n                sum += abs(val);\n                minVal = min(minVal, (long long)abs(val));\n            }\n        }\n\n        if (neg % 2 == 1) {\n            sum -= 2LL * minVal;\n        }\n\n        return sum;\n    }\n};\n```\n\n```python []\nclass Solution:\n    def maxMatrixSum(self, matrix: List[List[int]]) -> int:\n        neg = 0\n        minVal = float(\'inf\')\n        sum_abs = 0\n\n        for row in matrix:\n            for val in row:\n                if val < 0:\n                    neg += 1\n                sum_abs += abs(val)\n                minVal = min(minVal, abs(val))\n\n        if neg % 2 == 1:\n            sum_abs -= 2 * minVal\n\n        return sum_abs\n```\n\n```java []\nclass Solution {\n    public long maxMatrixSum(int[][] matrix) {\n        int neg = 0;\n        long sum = 0;\n        long minVal = Long.MAX_VALUE;\n\n        for (int[] row : matrix) {\n            for (int val : row) {\n                if (val < 0) {\n                    neg++;\n                }\n                sum += Math.abs(val);\n                minVal = Math.min(minVal, Math.abs(val));\n            }\n        }\n\n        if (neg % 2 == 1) {\n            sum -= 2 * minVal;\n        }\n\n        return sum;\n    }\n}\n```\n\n```javascript []\nclass Solution {\n    maxMatrixSum(matrix) {\n        let neg = 0;\n        let sum = 0;\n        let minVal = Infinity;\n\n        for (let row of matrix) {\n            for (let val of row) {\n                if (val < 0) {\n                    neg++;\n                }\n                sum += Math.abs(val);\n                minVal = Math.min(minVal, Math.abs(val));\n            }\n        }\n\n        if (neg % 2 === 1) {\n            sum -= 2 * minVal;\n        }\n\n        return sum;\n    }\n}\n```\n\n---\n\n### Explanation:\n1. **Variable Initialization**:\n   - `neg`: Tracks the number of negative elements.\n   - `sum`: Stores the total sum of absolute values.\n   - `minVal`: Tracks the smallest absolute value.\n\n2. **Matrix Traversal**:\n   - Loop through each element in the matrix.\n   - Count negatives (`neg`) and calculate the sum of absolute values (`sum`).\n   - Update `minVal` with the smallest absolute value.\n\n3. **Odd Negatives Adjustment**:\n   - If the count of negatives is odd, subtract twice the smallest absolute value from the sum.\n\n4. **Return Result**:\n   - Return the adjusted sum as the result.\n\n---\n\n# Approach\n- **Step 1**: Traverse the matrix to calculate `neg`, `sum`, and `minVal`.\n- **Step 2**: Adjust the `sum` if `neg` is odd.\n- **Step 3**: Return the result.\n\n---\n\n# Complexity\n- **Time Complexity**: \\(O(n \\times m)\\), where \\(n\\) is the number of rows and \\(m\\) is the number of columns in the matrix.\n- **Space Complexity**: \\(O(1)\\), as no additional space is used other than variables.\n\n---\n\n### *If you have any questions or need further clarification, feel free to drop a comment! \uD83D\uDE0A*
['Array', 'Two Pointers', 'Greedy', 'Matrix', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']