Minimum increments required to make given matrix palindromic | 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
def palindromeMatrix ( N , M , arr ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( ( N + 1 ) // 2 ) : NEW_LINE INDENT for j in range ( ( M + 1 ) // 2 ) : NEW_LINE INDENT s = { } NEW_LINE s [ ( i , j ) ] = 1 NEW_LINE s [ ( i , M - j - 1 ) ] = 1 NEW_LINE s [ ( N - i - 1 , j ) ] = 1 NEW_LINE s [ ( N - i - 1 , M - j - 1 ) ] = 1 NEW_LINE values = [ ] NEW_LINE for p , q in s : NEW_LINE INDENT values . append ( arr [ p ] [ q ] ) NEW_LINE DEDENT maxm = max ( values ) NEW_LINE for k in range ( len ( values ) ) : NEW_LINE INDENT ans += maxm - values [ k ] NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , M = 3 , 3 NEW_LINE arr = [ [ 1 , 2 , 1 ] , [ 3 , 4 , 1 ] , [ 1 , 2 , 1 ] ] NEW_LINE palindromeMatrix ( N , M , arr ) NEW_LINE DEDENT