Open In App

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

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • Let x be an element of the matrix is at position m[i][j] of 0-based indexing, then x will be at m[j][n-i-1] now if rotate matrix by 90 degrees, then x will be at m[n-i-1][n-j-1] now if rotate matrix by 180 degrees and then x will be at m[n-j-1][i] now if rotate matrix by 270 degrees.
  • we will iterate half rows and half column using two loops, we need not iterate the whole matrix because iterating half rows and half column, will cover all matrix elements by making a group such that one group have four elements that will interchange with each other while rotating the matrix.
  • Now, our task is to make all four elements of a particular group equal by converting all four elements to 0 or 1
  • So the minimum operation required for a particular group will be min( sums, 4-sums ), where sums are the sum of four elements of the particular group.
  • Then we will find the minimum operation required for all groups and add all of them.
  • Then return the final answer.

Below is the implementation of the above approach: 

C++




// 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




// 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)


Python3




# 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)


C#




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




// 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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads