Maximizing Matrix Sum by Flipping Negative Elements || Bruteforce Solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\nThe goal is to maximize the sum of the matrix by potentially flipping some negative v
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThe goal is to maximize the sum of the matrix by potentially flipping some negative values to positive ones. The key insight here is:\n1. If there\u2019s an **even** number of negative numbers, flipping them all results in a non-negative sum, so we can directly return the sum of absolute values.\n2. If the count of negative numbers is **odd**, flipping one of the negatives results in an imbalance. To fix this, we would "flip" the smallest negative value to positive by subtracting twice its absolute value, which maximizes the sum.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Traverse the Matrix:**\n   - Calculate the total sum of absolute values of all elements.\n   - Keep track of how many negative numbers are in the matrix.\n   - Find the smallest absolute value of any element to help in decision-making when the count of negative numbers is odd.\n\n2. **Handle the Case of Odd Number of Negative Elements:**\n   - If there\u2019s an even number of negative elements, no further adjustments are needed; just return the total sum of absolute values.\n   - If the count is odd, subtract twice the smallest absolute value from the total sum to "flip" the smallest negative value and maximize the sum.\n\n# Complexity\n- Time complexity : $$O(m \\times n)$$ where `m` is the number of rows and `n` is the number of columns in the matrix. We need to traverse the matrix once to compute the sum and gather the required information.\n\n- Space complexity : $$O(1)$$ since we only use a constant amount of additional space, regardless of the size of the input matrix.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    long long maxMatrixSum(vector<vector<int>>& matrix) {\n        //first of all i have find total abs sum of the matrix\n        long long totalSum = 0;\n        int minNeg = INT_MAX;\n        int negCount = 0;\n\n        for(int i=0; i<matrix.size(); i++){\n            for(int j=0; j<matrix[i].size() ;j++){\n\n                int val =   matrix[i][j];\n\n                if(val<0){\n                    negCount++;\n                }\n                \n                val = abs(matrix[i][j]);\n                totalSum+=val;\n                if( val<minNeg){\n                    minNeg = val;\n                }\n            }\n        }\n\n       \n        \n\n       \n        //ydi negCount even hai to simple return totalSum\n        if(negCount%2 == 0){\n            return totalSum;\n        }\n\n        //else totalSum me se minimum negative value remove kar do;\n        return totalSum- abs(2*minNeg);\n\n    }\n};\n```
['Array', 'Greedy', 'Matrix', 'C++']