Skip to content
Related Articles

Related Articles

Minimum flips to make Matrix identical for all rotations

View Discussion
Improve Article
Save Article
  • Last Updated : 16 Aug, 2022
View Discussion
Improve Article
Save Article

Given a binary matrix Mat[][] of size N*N, the task is to find the minimum number of flips to be performed such that the matrix is identical for all rotations.

Examples:

Input: Mat[][] = {{1, 0, 0}, {0, 1, 0}, {1, 0, 1}}
Output: 1
Explanation:  Change the element at row = 1, col =  3 from 0 to 1.
Now for all the rotations the matrix is identical.

Input: {{0}}
Output: 0

 

Approach: To solve the problem follow the below idea:

Rotate the matrix 4 times and for every position check how many of elements needs to be changed based on their positions in each rotation.

Follow the steps mentioned below to implement the observation:

  • Initialize a matrix of size N2 with 0 to store the count of 1 at that position for every rotation.
  • Rotate the matrix 4 times and count 1 for every position of the matrix.
  • Initialize Sum = 0 and count min(count of 1, count of 0) for every position of Mat[][].
  • Return Sum/4, as we count the same position 4 times.

Below is the implementation of the above approach:

C++




// C++ code for the above approach:
 
#include <bits/stdc++.h>
using namespace std;
 
// After transpose we swap
// elements of column
// one by one for finding left
// rotation of matrix
// by 90 degree
void reverseColumns(vector<vector<bool> >& arr)
{
    int R = arr.size();
    int C = arr[0].size();
 
    for (int i = 0; i < C; i++)
        for (int j = 0, k = C - 1; j < k; j++, k--)
            swap(arr[j][i], arr[k][i]);
}
 
// Function to do transpose of matrix
void transpose(vector<vector<bool> >& arr)
{
    int R = arr.size();
    int C = arr[0].size();
 
    for (int i = 0; i < R; i++)
        for (int j = i; j < C; j++)
            swap(arr[i][j], arr[j][i]);
}
 
// Function to rotate matrix
// anticlockwise by 90 degree
void rotate(vector<vector<bool> >& arr)
{
    transpose(arr);
    reverseColumns(arr);
}
 
// Function to return the minimum number of flips
int solve(vector<vector<bool> > v, int n)
{
    int Sum = 0;
 
    // Initialize Count vector
    vector<vector<int> > c(n, vector<int>(n, 0));
 
    // Count 1 in each position for every rotation
    for (int t = 0; t < 4; t++) {
        rotate(v);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (v[i][j] == 1)
                    c[i][j]++;
            }
        }
    }
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            Sum += min(c[i][j], 4 - c[i][j]);
        }
    }
 
    // Count Minimum reversal
    return Sum / 4;
}
 
// Driver Code
int main()
{
    vector<vector<bool> > v
        = { { 1, 0, 0 }, { 0, 1, 0 }, { 1, 0, 1 } };
 
    int N = 3;
 
    // Function call
    cout << solve(v, N) << endl;
    return 0;
}


Java




// Java code to implement the above approach
import java.util.*;
 
class GFG {
 
 
  // After transpose we swap
  // elements of column
  // one by one for finding left
  // rotation of matrix
  // by 90 degree
  static void reverseColumns(int[][] arr)
  {
    int R = arr.length;
    int C = arr[0].length;
 
    for (int i = 0; i < C; i++){
      for (int j = 0, k = C - 1; j < k; j++, k--){
        int temp = arr[j][i];
        arr[j][i] = arr[k][i];
        arr[k][i] = temp;
      }
    }
  }
 
  // Function to do transpose of matrix
  static void transpose(int[][] arr)
  {
    int R = arr.length;
    int C = arr[0].length;
 
    for (int i = 0; i < R; i++){
      for (int j = i; j < C; j++){
        int temp = arr[j][i];
        arr[j][i] = arr[i][j];
        arr[i][j] = temp;
      }
    }
  }
 
  // Function to rotate matrix
  // anticlockwise by 90 degree
  static void rotate(int[][] arr)
  {
    transpose(arr);
    reverseColumns(arr);
  }
 
  // Function to return the minimum number of flips
  static int solve(int[][] v, int n)
  {
    double Sum = 0.0;
 
    // Initialize Count vector
    int[][] c = new int[n][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        c[i][j] = 1;
      }
    }
 
    // Count 1 in each position for every rotation
    for (int t = 0; t < 4; t++) {
      rotate(v);
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (v[i][j] == 1)
            c[i][j]++;
        }
      }
    }
 
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        Sum += Math.min(c[i][j], 4 - c[i][j]);
      }
    }
 
    // Count Minimum reversal
    return (int)Math.round(Sum / 4);
  }
 
  // Driver Code
  public static void main (String[] args) {
 
    int[][] v
      = { { 1, 0, 0 }, { 0, 1, 0 }, { 1, 0, 1 } };
 
    int N = 3;
 
    // Function call
    System.out.println(solve(v, N));
  }
}
 
// This code is contributed by sanjoy_62.


C#




// C# code to implement the above approach
using System;
 
public class GFG{
 
  // After transpose we swap
  // elements of column
  // one by one for finding left
  // rotation of matrix
  // by 90 degree
  static void reverseColumns(int[,] arr)
  {
    int C = arr.GetLength(1);
 
    for (int i = 0; i < C; i++){
      for (int j = 0, k = C - 1; j < k; j++, k--){
        int temp = arr[j ,i];
        arr[j, i] = arr[k ,i];
        arr[k, i] = temp;
      }
    }
  }
 
  // Function to do transpose of matrix
  static void transpose(int[, ] arr)
  {
    int R = arr.GetLength(0);
    int C = arr.GetLength(1);
 
    for (int i = 0; i < R; i++){
      for (int j = i; j < C; j++){
        int temp = arr[j,i];
        arr[j,i] = arr[i,j];
        arr[i,j] = temp;
      }
    }
  }
 
  // Function to rotate matrix
  // anticlockwise by 90 degree
  static void rotate(int[,] arr)
  {
    transpose(arr);
    reverseColumns(arr);
  }
 
  // Function to return the minimum number of flips
  static int solve(int[,] v, int n)
  {
    double Sum = 0.0;
 
    // Initialize Count vector
    int[,] c = new int[n,n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        c[i,j] = 1;
      }
    }
 
    // Count 1 in each position for every rotation
    for (int t = 0; t < 4; t++) {
      rotate(v);
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (v[i,j] == 1)
            c[i,j]++;
        }
      }
    }
 
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        Sum += Math.Min(c[i,j], 4 - c[i,j]);
      }
    }
 
    // Count Minimum reversal
    return (int)Math.Round(Sum / 4);
  }
 
  // Driver Code
  static public void Main (){
 
    int[,] v
      = new int[,] { { 1, 0, 0 }, { 0, 1, 0 }, { 1, 0, 1 } };
 
    int N = 3;
 
    // Function call
    Console.Write(solve(v, N));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript




<script>
    // JavaScript code for the above approach:
 
 
    // After transpose we swap
    // elements of column
    // one by one for finding left
    // rotation of matrix
    // by 90 degree
    const reverseColumns = (arr) => {
        let R = arr.length;
        let C = arr[0].length;
 
        for (let i = 0; i < C; i++)
            for (let j = 0, k = C - 1; j < k; j++, k--) {
                let temp = arr[j][i];
                arr[j][i] = arr[k][i];
                arr[k][i] = temp;
            }
    }
 
    // Function to do transpose of matrix
    const transpose = (arr) => {
        let R = arr.length;
        let C = arr[0].length;
 
        for (let i = 0; i < R; i++)
            for (let j = i; j < C; j++) {
                let temp = arr[j][i];
                arr[j][i] = arr[i][j];
                arr[i][j] = temp;
            }
    }
 
    // Function to rotate matrix
    // anticlockwise by 90 degree
    const rotate = (arr) => {
        transpose(arr);
        reverseColumns(arr);
    }
 
    // Function to return the minimum number of flips
    const solve = (v, n) => {
        let Sum = 0;
 
        // Initialize Count vector
        let c = new Array(n).fill(0).map(() => new Array(n).fill(0));
 
        // Count 1 in each position for every rotation
        for (let t = 0; t < 4; t++) {
            rotate(v);
            for (let i = 0; i < n; i++) {
                for (let j = 0; j < n; j++) {
                    if (v[i][j] == 1)
                        c[i][j]++;
                }
            }
        }
 
        for (let i = 0; i < n; i++) {
            for (let j = 0; j < n; j++) {
                Sum += Math.min(c[i][j], 4 - c[i][j]);
            }
        }
 
        // Count Minimum reversal
        return parseInt(Sum / 4);
    }
 
    // Driver Code
 
    let v = [[1, 0, 0], [0, 1, 0], [1, 0, 1]];
 
    let N = 3;
 
    // Function call
    document.write(solve(v, N));
 
// This code is contributed by rakeshsahni
 
</script>


Output

1

Time Complexity: O(N2)
Auxiliary Space: O(N2)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!