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++
// CPP Program to find minimum flip required to make // Binary Matrix symmetric along main diagonal #include <bits/stdc++.h> #define N 3 using namespace std; // Return the minimum flip required to make // Binary Matrix symmetric along main diagonal. int minimumflip(int mat[][N], int n) { int transpose[n][n]; // finding the transpose of the matrix for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) transpose[i][j] = mat[j][i]; // Finding the number of position where // element are not same. 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; } // Driver Program 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
// Java Program to find minimum flip // required to make Binary Matrix // symmetric along main diagonal import java.util.*; class GFG { // Return the minimum flip required // to make Binary Matrix symmetric // along main diagonal. static int minimumflip(int mat[][], int n) { int transpose[][] = new int[n][n]; // finding the transpose of the matrix for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) transpose[i][j] = mat[j][i]; // Finding the number of position // where element are not same. 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; } /* Driver program to test above function */ 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)); } } // This code is contributed by Arnav Kr. Mandal. |
Python3
# Python3 code to find minimum flip # required to make Binary Matrix # symmetric along main diagonal N = 3 # Return the minimum flip required # to make Binary Matrix symmetric # along main diagonal. def minimumflip(mat, n): transpose =[[0] * n] * n # finding the transpose of the matrix for i in range(n): for j in range(n): transpose[i][j] = mat[j][i] # Finding the number of position # where element are not same. 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) # Driver Program n = 3mat =[[ 0, 0, 1], [ 1, 1, 1], [ 1, 0, 0]] print( minimumflip(mat, n)) # This code is contributed by "Sharad_Bhardwaj". |
C#
// C# Program to find minimum flip // required to make Binary Matrix // symmetric along main diagonal using System; class GFG { // Return the minimum flip required // to make Binary Matrix symmetric // along main diagonal. static int minimumflip(int [,]mat, int n) { int [,]transpose = new int[n,n]; // finding the transpose of the matrix for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) transpose[i,j] = mat[j,i]; // Finding the number of position // where element are not same. 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; } /* Driver program to test above function */ public static void Main() { int n = 3; int [,]mat = {{ 0, 0, 1 }, { 1, 1, 1 }, { 1, 0, 0 }}; Console.WriteLine(minimumflip(mat, n)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP Program to find minimum // flip required to make $N = 3; // Return the minimum flip // required to make Binary // Matrix symmetric along // main diagonal. function minimumflip($mat, $n) { global $N; $transpose; // finding the transpose // of the matrix for ( $i = 0; $i < $n; $i++) for ($j = 0; $j < $n; $j++) $transpose[$i][$j] = $mat[$j][$i]; // Finding the number of // position where element // are not same. $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; } // Driver Code $n = 3; $mat = array(array(0, 0, 1), array(1, 1, 1), array(1, 0, 0)); echo minimumflip($mat, $n),"\n"; // This code is contributed by aj_36 ?> |
Output :
2
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 theya re not equal will be the minimum flip required to make matrix symmetric along main diagonal.
Below is the implementation of this approach:
C++
// CPP Program to find minimum flip required to make // Binary Matrix symmetric along main diagonal #include <bits/stdc++.h> #define N 3 using namespace std; // Return the minimum flip required to make // Binary Matrix symmetric along main diagonal. int minimumflip(int mat[][N], int n) { // Comparing elements across diagonal 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; } // Driver Program 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
// Java Program to find minimum flip // required to make Binary Matrix // symmetric along main diagonal import java.util.*; class GFG { // Return the minimum flip required // to make Binary Matrix symmetric // along main diagonal. static int minimumflip(int mat[][], int n) { // Comparing elements across diagonal 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; } /* Driver program to test above function */ 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)); } } // This code is contributed by Arnav Kr. Mandal. |
Python3
# Python3 code to find minimum flip # required to make Binary Matrix # symmetric along main diagonal N = 3 # Return the minimum flip required # to make Binary Matrix symmetric # along main diagonal. def minimumflip( mat , n ): # Comparing elements across diagonal flip = 0 for i in range(n): for j in range(i): if mat[i][j] != mat[j][i] : flip += 1 return flip # Driver Program n = 3mat =[[ 0, 0, 1], [ 1, 1, 1], [ 1, 0, 0]] print( minimumflip(mat, n)) # This code is contributed by "Sharad_Bhardwaj". |
C#
// C# Program to find minimum flip // required to make Binary Matrix // symmetric along main diagonal using System; class GFG { // Return the minimum flip required // to make Binary Matrix symmetric // along main diagonal. static int minimumflip(int [,]mat, int n) { // Comparing elements across diagonal 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; } /* Driver program to test above function */ public static void Main() { int n = 3; int [,]mat = {{ 0, 0, 1 }, { 1, 1, 1 }, { 1, 0, 0 }}; Console.WriteLine(minimumflip(mat, n)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP Program to find minimum // flip required to make Binary // Matrix symmetric along main diagonal $N = 3; // Return the minimum flip // required to make Binary // Matrix symmetric along main diagonal. function minimumflip($mat, $n) { // Comparing elements // across diagonal $flip = 0; for ($i = 0; $i < $n; $i++) for ($j = 0; $j < $i; $j++) if ($mat[$i][$j] != $mat[$j][$i]) $flip++; return $flip; } // Driver Code $n = 3; $mat = array(array(0, 0, 1), array(1, 1, 1), array(1, 0, 0)); echo minimumflip($mat, $n), "\n"; // This code is contributed by ajit ?> |
Output :
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- A square matrix as sum of symmetric and skew-symmetric matrices
- Horizontally Flip a Binary Matrix
- Program to check if a matrix is symmetric
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- C Program To Check whether Matrix is Skew Symmetric or not
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Minimum operations required to make each row and column of matrix equals
- Minimum changes required to make each path in a matrix palindrome
- Minimum increments required to make given matrix palindromic
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimum subarray reversals required to make given binary array alternating
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Minimum operations required to set all elements of binary matrix
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Minimum number of operations required to set all elements of a binary matrix
- Number of Symmetric Relations on a Set
- Count of minimum reductions required to get the required sum K
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all Array elements divisible by K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t

