Open In App

Minimum operations in a binary matrix to make it same as initial one

Given a square binary matrix[][] of size n*n. Find the minimum number of operations (changes) needed in a binary matrix to make it the same as the initial one after rotating 0, 90, 180 and 270 degrees. In one operation, You can choose an element of the matrix and change it from 0 to 1 or from 1 to 0.

Examples:



Input: N = 3, mat[][] = {{1, 1, 0},  {1, 1, 0}, {0, 1, 0}}
Output: 2
Explanation: change 1 to 0 in M[1][1] and change 0 to 1 in M[2][3].

Input: N = 4, mat[][] = {{1, 1, 1, 0}, {1, 0, 0, 0},  {0, 1, 0, 1},  {0, 1, 0, 1}} 
Output: 4
Explanation: change from 1 to 0 in M[1][2], M[3][2] change from 0 to 1 in M[1][4], M[4][1]  



Illustration:

Let’s consider 1st Example:

 N = 3

 1 1 0 
 1 1 0
 0 1 0

  • After rotating matrix 90 degree, m[1][1] will become m[1][3]
  • After rotating matrix 180 degree m[1][1] will become m[3][3]
  • After rotating matrix 270 degree m[1][1] will become m[3][1]
  • Means m[1][1], m[1][3], m[3][3], and m[3][1] will interchange with each other if we rotate the matrix 0, 90, 180, and 270 degrees . So, we have to make these four elements equal and we can make these four elements equal by converting all four elements to 0 or 1.
  • So, we can split all matrix elements into groups such that one group has four elements that will interchange with each other.

Steps Involved in the implementation of the code.

Below is the implementation of the above approach: 




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
#define n 3
  
// Function to find minimum number of
// operations required in a binary matrix
// that remains the same after rotating
// 0, 90, 180 and 270 degree
void EqualMatrix(int m[n][n])
{
  
    // Initializing our ans to zero
    int ans = 0, sum;
  
    // Iterating half row of the matrix
    // because we need not  to iterate all
    // rows, iterating half row will find
    // answer for all rows  in the matrix
    for (int i = 0; i < ceil((n * 1.00) / 2); i++) {
  
        // Iterating half column of the
        // matrix because we need not to
        // iterate all column, iterating
        // half column will find answer
        // for all column in the matrix
        for (int j = 0; j < n / 2; j++) {
  
            // Adding sum of four elements
            // of a same group
            sum = m[i][j] + m[j][n - i - 1]
                  + m[n - i - 1][n - j - 1]
                  + m[n - j - 1][i];
  
            // Adding minimum operation to
            // our answer
            ans += min(sum, 4 - sum);
        }
    }
  
    // Print our final answer
    cout << ans << endl;
}
  
// Drive code
int main()
{
  
    // Input matrix
    int m[n][n] = { { 1, 1, 0 }, { 1, 1, 0 }, { 0, 1, 0 } };
  
    // Function call
    EqualMatrix(m);
  
    return 0;
}




// Java program for the above approach
import java.util.*;
 
class GFG {
  static int n = 3;
 
  // Function to find minimum number of
  // operations required in a binary matrix
  // that remains the same after rotating
  // 0, 90, 180 and 270 degree
  static void EqualMatrix(int[][] m)
  {
 
    // Initializing our ans to zero
    int ans = 0, sum;
 
    // Iterating half row of the matrix
    // because we need not to iterate all
    // rows, iterating half row will find
    // answer for all rows in the matrix
    for (int i = 0; i < Math.ceil((n * 1.00) / 2);
         i++) {
 
      // Iterating half column of the
      // matrix because we need not to
      // iterate all column, iterating
      // half column will find answer
      // for all column in the matrix
      for (int j = 0; j < n / 2; j++) {
 
        // Adding sum of four elements
        // of a same group
        sum = m[i][j] + m[j][n - i - 1]
          + m[n - i - 1][n - j - 1]
          + m[n - j - 1][i];
 
        // Adding minimum operation to
        // our answer
        ans += Math.min(sum, 4 - sum);
      }
    }
 
    // Print our final answer
    System.out.println(ans);
  }
 
  // Drive code
  public static void main(String[] args)
  {
 
    // Input matrix
    int[][] m
      = { { 1, 1, 0 }, { 1, 1, 0 }, { 0, 1, 0 } };
 
    // Function call
    EqualMatrix(m);
  }
}
 
// This code is contributed by Prasad Kandekar(prasad264)




# Python program for the above approach
 
import math
 
# Function to find minimum number of
# operations required in a binary matrix
# that remains the same after rotating
# 0, 90, 180 and 270 degree
 
 
def EqualMatrix(m):
 
    # Initializing our ans to zero
    ans = 0
 
    # Iterating half row of the matrix
    # because we need not  to iterate all
    # rows, iterating half row will find
    # answer for all rows  in the matrix
    for i in range(math.ceil(n/2)):
 
        # Iterating half column of the
        # matrix because we need not to
        # iterate all column, iterating
        # half column will find answer
        # for all column in the matrix
        for j in range(n//2):
 
            # Adding sum of four elements
            # of a same group
            sum = m[i][j] + m[j][n - i - 1] + \
                m[n - i - 1][n - j - 1] + m[n - j - 1][i]
 
            # Adding minimum operation to
            # our answer
            ans += min(sum, 4 - sum)
 
    # Print our final answer
    print(ans)
 
 
# Input matrix
n = 3
m = [[1, 1, 0], [1, 1, 0], [0, 1, 0]]
 
# Function call
EqualMatrix(m)




using System;
 
class Program
{
    const int n = 3;
 
    static void EqualMatrix(int[,] m)
    {
        int ans = 0, sum;
 
        for (int i = 0; i < Math.Ceiling((n * 1.00) / 2); i++)
        {
            for (int j = 0; j < n / 2; j++)
            {
                sum = m[i, j] + m[j, n - i - 1]
                      + m[n - i - 1, n - j - 1]
                      + m[n - j - 1, i];
 
                ans += Math.Min(sum, 4 - sum);
            }
        }
 
        Console.WriteLine(ans);
    }
 
    static void Main()
    {
        int[,] m = { { 1, 1, 0 }, { 1, 1, 0 }, { 0, 1, 0 } };
 
        EqualMatrix(m);
    }
}




// JavaScript program for the above approach
 
// Function to find minimum number of
// operations required in a binary matrix
// that remains the same after rotating
// 0, 90, 180 and 270 degree
function EqualMatrix(m) {
 
    // Initializing our ans to zero
    let ans = 0;
    let sum;
 
    // Iterating half row of the matrix
    // because we need not to iterate all
    // rows, iterating half row will find
    // answer for all rows in the matrix
    for (let i = 0; i < Math.ceil(n / 2); i++) {
 
        // Iterating half column of the
        // matrix because we need not to
        // iterate all column, iterating
        // half column will find answer
        // for all column in the matrix
        for (let j = 0; j < Math.floor(n / 2); j++) {
 
            // Adding sum of four elements
            // of a same group
            sum = m[i][j] + m[j][n - i - 1] +
                m[n - i - 1][n - j - 1] + m[n - j - 1][i];
 
            // Adding minimum operation to
            // our answer
            ans += Math.min(sum, 4 - sum);
        }
    }
 
    // Print our final answer
    console.log(ans);
}
 
// Input matrix
const n = 3;
const m = [[1, 1, 0], [1, 1, 0], [0, 1, 0]];
 
// Function call
EqualMatrix(m);
 
// This code is contributed by prasad264

Output
2

Time Complexity: O(n2)
Auxiliary Space: O(1)


Article Tags :