Minimum increments required to make given matrix palindromic | Java 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
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void palindromeMatrix ( int N , int M , int [ ] [ ] arr ) { int ans = 0 ; for ( int i = 0 ; i < ( N + 1 ) / 2 ; i ++ ) { for ( int j = 0 ; j < ( M + 1 ) / 2 ; j ++ ) { HashSet < pair > s = new HashSet < > ( ) ; 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 ) ) ; Vector < Integer > values = new Vector < > ( ) ; for ( pair p : s ) { values . add ( arr [ p . first ] [ p . second ] ) ; } int max = Collections . max ( values ) ; for ( int k = 1 ; k < values . size ( ) ; k ++ ) { ans += max - values . get ( k ) ; } } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int N = 3 , M = 3 ; int [ ] [ ] arr = { { 1 , 2 , 1 } , { 3 , 4 , 1 } , { 1 , 2 , 1 } } ; palindromeMatrix ( N , M , arr ) ; } }