Maximize matrix sum by repeatedly multiplying pairs of adjacent elements with | C # program to implement the above approach ; Function to calculate maximum sum possible of a matrix by multiplying pairs of adjacent elements with - 1 any number of times ( possibly zero ) ; Store the maximum sum of matrix possible ; Stores the count of negative values in the matrix ; Store minimum absolute value present in the matrix ; Traverse the matrix row - wise ; Update sum ; If current element is negative , increment the negative count ; If current value is less than the overall minimum in A [ ] , update the overall minimum ; If there are odd number of negative values , then the answer will be sum of the absolute values of all elements - 2 * minVal ; Print maximum sum ; Driver Code ; Given matrix ; Dimensions of matrix
using System ; class GFG { static void getMaxSum ( int [ , ] A , int M , int N ) { int sum = 0 ; int negative = 0 ; int minVal = Int32 . MaxValue ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { sum += Math . Abs ( A [ i , j ] ) ; if ( A [ i , j ] < 0 ) { negative ++ ; } minVal = Math . Min ( minVal , Math . Abs ( A [ i , j ] ) ) ; } } if ( negative % 2 != 0 ) { sum -= 2 * minVal ; } Console . Write ( sum ) ; } public static void Main ( ) { int [ , ] A = { { 4 , - 8 , 6 } , { 3 , 7 , 2 } } ; int M = A . GetLength ( 0 ) ; int N = A . GetLength ( 1 ) ; getMaxSum ( A , M , N ) ; } }