VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-flips-to-make-matrix-identical-for-all-rotations/

⇱ Minimum flips to make Matrix identical for all rotations - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum flips to make Matrix identical for all rotations

Last Updated : 7 Oct, 2022

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:


Output
1

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

Comment
Article Tags: