Minimum increments required to make given matrix palindromic | Javascript program for the above approach ; Function to evaluate minimum number of operation required to convert the matrix to a palindrome matrix ; Variable to store number of operations required ; Iterate over the first quadrant of the matrix ; Store positions of all four values from four quadrants ; Store the values having unique indexes ; Largest value in the values vector ; Evaluate minimum increments required to make all vector elements equal ; Print the answer ; Driver Code ; Function Call
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function palindromeMatrix ( N , M , arr ) { let ans = 0 ; for ( let i = 0 ; i < Math . floor ( ( N + 1 ) / 2 ) ; i ++ ) { for ( let j = 0 ; j < Math . floor ( ( M + 1 ) / 2 ) ; j ++ ) { let s = new Set ( ) ; s . add ( new pair ( i , j ) ) ; s . add ( new pair ( i , M - j - 1 ) ) ; s . add ( new pair ( N - i - 1 , j ) ) ; s . add ( new pair ( N - i - 1 , M - j - 1 ) ) ; let values = [ ] ; for ( let p of s . values ( ) ) { values . push ( arr [ p . first ] [ p . second ] ) ; } values . sort ( function ( a , b ) { return a - b ; } ) ; let max = Math . max ( ... values ) ; for ( let k = 1 ; k < values . length ; k ++ ) { ans += max - values [ k ] ; } } } document . write ( ans ) ; } let N = 3 , M = 3 ; let arr = [ [ 1 , 2 , 1 ] , [ 3 , 4 , 1 ] , [ 1 , 2 , 1 ] ] ; palindromeMatrix ( N , M , arr ) ;