Flipping the Matrix

Flipping the Matrix

·

1 min read

Given a 2n*2n matrix. You can flip the entire row and entire column as many times as you want, you have to make the first quadrant value maximum after a certain number of operations. Return the sum of the first quadrant after flipping the matrix's columns and rows.

Example:

public static int flippingMatrix(List<List<Integer>> matrix) {

     int m = matrix.size();
     int n = matrix.get(0).size();
     int sum = 0;
    // we are only traversing first quadrant elements as that is where we have to get the max sum
     for(int i=0; i<m/2; i++) {
         for(int j=0; j<n/2; j++) {
        // making sure we get the maximum element at all the symmetrical points
            //The original element
             int max = matrix.get(i).get(j); 
            // compare with element in same row but symmetrical side
             if(max < matrix.get(i).get(n-1-j)) 
                 max = matrix.get(i).get(n-1-j); 
            // comparing with element in same column but symmetrical side
             if(max < matrix.get(m-1-i).get(j))
                 max = matrix.get(m-1-i).get(j);
            // Comparing with elment in symmetrical row and column
             if(max < matrix.get(m-1-i).get(n-1-j))
                 max = matrix.get(m-1-i).get(n-1-j);

             sum += max; // adding the final maximum element to the sum that we have to return
         }
     }
     return sum;
}