Related Articles

# Minimum flip required to make Binary Matrix symmetric

• Difficulty Level : Basic
• Last Updated : 30 Apr, 2021

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 is not equal to mat.
Value of mat is not equal to mat.
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 ``#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 ``=` `3``mat ``=``[[ ``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

 ``

## Javascript

 ``

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 ``#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 ``=` `3``mat ``=``[[ ``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

 ``

## Javascript

 ``

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up