Minimum flip required to make Binary Matrix symmetric
Last Updated :
19 Aug, 2022
Given a Binary Matrix of size N X N, consisting of 1s and 0s. The task is to find the minimum flips required to make the matrix symmetric along main diagonal.
Examples :
Input : mat[][] = { { 0, 0, 1 },
{ 1, 1, 1 },
{ 1, 0, 0 } };
Output : 2
Value of mat[1][0] is not equal to mat[0][1].
Value of mat[2][1] is not equal to mat[1][2].
So, two flip are required.
Input : mat[][] = { { 1, 1, 1, 1, 0 },
{ 0, 1, 0, 1, 1 },
{ 1, 0, 0, 0, 1 },
{ 0, 1, 0, 1, 0 },
{ 0, 1, 0, 0, 1 } };
Output : 3
Method 1 (Simple):
The idea is to find the transpose of the matrix and find minimum number of flip required to make transpose and original matrix equal. To find minimum flip, find the number of position where original matrix and transpose matrix are not same, say x. So, our answer will be x/2.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
#define N 3
using namespace std;
int minimumflip( int mat[][N], int n)
{
int transpose[n][n];
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
transpose[i][j] = mat[j][i];
int flip = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
if (transpose[i][j] != mat[i][j])
flip++;
return flip / 2;
}
int main()
{
int n = 3;
int mat[N][N] = {
{ 0, 0, 1 },
{ 1, 1, 1 },
{ 1, 0, 0 }
};
cout << minimumflip(mat, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int minimumflip( int mat[][], int n)
{
int transpose[][] = new int [n][n];
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < n; j++)
transpose[i][j] = mat[j][i];
int flip = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < n; j++)
if (transpose[i][j] != mat[i][j])
flip++;
return flip / 2 ;
}
public static void main(String[] args)
{
int n = 3 ;
int mat[][] = {{ 0 , 0 , 1 },
{ 1 , 1 , 1 },
{ 1 , 0 , 0 }};
System.out.println(minimumflip(mat, n));
}
}
|
Python3
N = 3
def minimumflip(mat, n):
transpose = [[ 0 ] * n] * n
for i in range (n):
for j in range (n):
transpose[i][j] = mat[j][i]
flip = 0
for i in range (n):
for j in range (n):
if transpose[i][j] ! = mat[i][j]:
flip + = 1
return int (flip / 2 )
n = 3
mat = [[ 0 , 0 , 1 ],
[ 1 , 1 , 1 ],
[ 1 , 0 , 0 ]]
print ( minimumflip(mat, n))
|
C#
using System;
class GFG {
static int minimumflip( int [,]mat, int n)
{
int [,]transpose = new int [n,n];
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
transpose[i,j] = mat[j,i];
int flip = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
if (transpose[i,j] != mat[i,j])
flip++;
return flip / 2;
}
public static void Main()
{
int n = 3;
int [,]mat = {{ 0, 0, 1 },
{ 1, 1, 1 },
{ 1, 0, 0 }};
Console.WriteLine(minimumflip(mat, n));
}
}
|
PHP
<?php
$N = 3;
function minimumflip( $mat , $n )
{
global $N ;
$transpose ;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 0; $j < $n ; $j ++)
$transpose [ $i ][ $j ] = $mat [ $j ][ $i ];
$flip = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 0; $j < $n ; $j ++)
if ( $transpose [ $i ][ $j ] != $mat [ $i ][ $j ])
$flip ++;
return $flip / 2;
}
$n = 3;
$mat = array ( array (0, 0, 1),
array (1, 1, 1),
array (1, 0, 0));
echo minimumflip( $mat , $n ), "\n" ;
?>
|
Javascript
<script>
function minimumflip(mat, n)
{
let transpose = new Array(n);
for ( var i = 0; i < transpose.length; i++) {
transpose[i] = new Array(2);
}
for (let i = 0; i < n; i++)
for (let j = 0; j < n; j++)
transpose[i][j] = mat[j][i];
let flip = 0;
for (let i = 0; i < n; i++)
for (let j = 0; j < n; j++)
if (transpose[i][j] != mat[i][j])
flip++;
return flip / 2;
}
let n = 3;
let mat = [[ 0, 0, 1 ],
[ 1, 1, 1 ],
[ 1, 0, 0 ]];
document.write(minimumflip(mat, n));
</script>
|
Output :
2
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Method 2: (Efficient Approach)
The idea is to find minimum flip required to make upper triangle of matrix equals to lower triangle of the matrix. To do so, we run two nested loop, outer loop from i = 0 to n i.e for each row of the matrix and the inner loop from j = 0 to i, and check whether mat[i][j] is equal to mat[j][i]. Count of number of instance where they are not equal will be the minimum flip required to make matrix symmetric along main diagonal.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
#define N 3
using namespace std;
int minimumflip( int mat[][N], int n)
{
int flip = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < i; j++)
if (mat[i][j] != mat[j][i])
flip++;
return flip;
}
int main()
{
int n = 3;
int mat[N][N] = {
{ 0, 0, 1 },
{ 1, 1, 1 },
{ 1, 0, 0 }
};
cout << minimumflip(mat, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int minimumflip( int mat[][], int n)
{
int flip = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < i; j++)
if (mat[i][j] != mat[j][i])
flip++;
return flip;
}
public static void main(String[] args)
{
int n = 3 ;
int mat[][] = {{ 0 , 0 , 1 },
{ 1 , 1 , 1 },
{ 1 , 0 , 0 }};
System.out.println(minimumflip(mat, n));
}
}
|
Python3
N = 3
def minimumflip( mat , n ):
flip = 0
for i in range (n):
for j in range (i):
if mat[i][j] ! = mat[j][i] :
flip + = 1
return flip
n = 3
mat = [[ 0 , 0 , 1 ],
[ 1 , 1 , 1 ],
[ 1 , 0 , 0 ]]
print ( minimumflip(mat, n))
|
C#
using System;
class GFG {
static int minimumflip( int [,]mat, int n)
{
int flip = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < i; j++)
if (mat[i,j] != mat[j,i])
flip++;
return flip;
}
public static void Main()
{
int n = 3;
int [,]mat = {{ 0, 0, 1 },
{ 1, 1, 1 },
{ 1, 0, 0 }};
Console.WriteLine(minimumflip(mat, n));
}
}
|
PHP
<?php
$N = 3;
function minimumflip( $mat , $n )
{
$flip = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 0; $j < $i ; $j ++)
if ( $mat [ $i ][ $j ] != $mat [ $j ][ $i ])
$flip ++;
return $flip ;
}
$n = 3;
$mat = array ( array (0, 0, 1),
array (1, 1, 1),
array (1, 0, 0));
echo minimumflip( $mat , $n ), "\n" ;
?>
|
Javascript
<script>
function minimumflip(mat, n)
{
let flip = 0;
for (let i = 0; i < n; i++)
for (let j = 0; j < i; j++)
if (mat[i][j] != mat[j][i])
flip++;
return flip;
}
let n = 3;
let mat = [[ 0, 0, 1 ],
[ 1, 1, 1 ],
[ 1, 0, 0 ]];
document.write(minimumflip(mat, n));
</script>
|
Output :
2
Time Complexity: O(N2)
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...