Minimum operations in a binary matrix to make it same as initial one
Last Updated :
06 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
#define n 3
void EqualMatrix( int m[n][n])
{
int ans = 0, sum;
for ( int i = 0; i < ceil ((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 += min(sum, 4 - sum);
}
}
cout << ans << endl;
}
int main()
{
int m[n][n] = { { 1, 1, 0 }, { 1, 1, 0 }, { 0, 1, 0 } };
EqualMatrix(m);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int n = 3 ;
static void EqualMatrix( int [][] m)
{
int ans = 0 , sum;
for ( int i = 0 ; i < Math.ceil((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);
}
}
System.out.println(ans);
}
public static void main(String[] args)
{
int [][] m
= { { 1 , 1 , 0 }, { 1 , 1 , 0 }, { 0 , 1 , 0 } };
EqualMatrix(m);
}
}
|
Python3
import math
def EqualMatrix(m):
ans = 0
for i in range (math.ceil(n / 2 )):
for j in range (n / / 2 ):
sum = m[i][j] + m[j][n - i - 1 ] + \
m[n - i - 1 ][n - j - 1 ] + m[n - j - 1 ][i]
ans + = min ( sum , 4 - sum )
print (ans)
n = 3
m = [[ 1 , 1 , 0 ], [ 1 , 1 , 0 ], [ 0 , 1 , 0 ]]
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
function EqualMatrix(m) {
let ans = 0;
let sum;
for (let i = 0; i < Math.ceil(n / 2); i++) {
for (let j = 0; j < Math.floor(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.log(ans);
}
const n = 3;
const m = [[1, 1, 0], [1, 1, 0], [0, 1, 0]];
EqualMatrix(m);
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...