Very Easy just Observe Even or Odd no. of -ve Elements in C++ || Runs 0ms in runtime with O(1) Space
\n\n\n# Approach\n Describe your approach to solving the problem. \n- If there are even number of negative elements in all matrix then we can just return the ab
![image.png](https://assets.leetcode.com/users/images/1379634d-418b-4137-901d-94749d0bab86_1732927123.1476867.png)\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- If there are even number of negative elements in all matrix then we can just return the absolute sum of matrix because in many operation we would be able to remove all negatives.\n- So, we don\'t need to perform operations.\n- But if no. of negative elements is Odd then it means we have to allow one negative number in all matrix and that should be smallest number in all matrix.\n- Because that would give us maximum sum\n\n# Complexity\n- Time complexity: **O(N*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```cpp []\n#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")\n#pragma GCC optimize ("O3", "unroll-loops", "Ofast", "-ffloat-store")\nstatic const auto harsh = []() { \n    ios_base::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n\nclass Solution {\npublic:\n    typedef long long ll;\n    ll maxMatrixSum(vector<vector<int>>& mat) {\n        int n = mat.size();  \n        ll temp = 0;\n        int numOfNegatives = 0;\n        int mini = INT_MAX;\n\n        for(int i = 0; i<n; i++){\n            for(int j = 0; j<n; j++){\n                temp += abs(mat[i][j]);\n                // Get the minimum element from all matrix\n                mini = min(mini, abs(mat[i][j]));\n                if(mat[i][j] < 0){\n                    // Count negative elements\n                    numOfNegatives++;\n                }\n            }\n        }\n\n// If count of negative is even just return absolute sum of matrix\n        if(!(numOfNegatives&1) ){\n            return temp;\n        }\n// Else return sum by subtracting minimum element\n        return temp - 2*mini;\n    }\n};\n```
['Array', 'Greedy', 'Matrix', 'C++']