Maximize matrix sum by flipping the sign of any adjacent pairs | Function to find the maximum sum of matrix element after flipping the signs of adjacent matrix elements ; Initialize row and column ; Stores the sum of absolute matrix element ; Find the minimum absolute value in the matrix ; Store count of negatives ; Find the smallest absolute value in the matrix ; Increment the count of negative numbers ; Find the absolute sum ; If the count of negatives is even then print the sum ; Subtract minimum absolute element ; Driver Code
function maxSum ( matrix ) { let r = matrix . length ; let c = matrix [ 0 ] . length ; let sum = 0 ; let mini = Number . MAX_VALUE ; let count = 0 ; for ( let i = 0 ; i < r ; i ++ ) { for ( let j = 0 ; j < c ; j ++ ) { let k = matrix [ i ] [ j ] ; mini = Math . min ( mini , Math . abs ( k ) ) ; if ( k < 0 ) count ++ ; sum += Math . abs ( k ) ; } } if ( count % 2 == 0 ) { return sum ; } else { return ( sum - 2 * mini ) ; } } let matrix = [ [ 2 , - 2 ] , [ - 2 , 2 ] ] ; document . write ( maxSum ( matrix ) ) ;