Open In App

Minimum flip required to make Binary Matrix symmetric

Last Updated : 19 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




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


Javascript




<script>
 
// JavaScript Program to find minimum flip
// required to make Binary Matrix
// symmetric along main diagonal
 
    // Return the minimum flip required
    // to make Binary Matrix symmetric
    // along main diagonal.
    function minimumflip(mat, n)
    {
        let transpose = new Array(n);
         
        // Loop to create 2D array using 1D array
        for (var i = 0; i < transpose.length; i++) {
            transpose[i] = new Array(2);
        }
        
        // finding the transpose of the matrix
        for (let i = 0; i < n; i++)
            for (let j = 0; j < n; j++)
                transpose[i][j] = mat[j][i];
        
        // Finding the number of position
        // where element are not same.
        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;
    }
 
// Driver code
 
        let n = 3;
        let mat = [[ 0, 0, 1 ],
                       [ 1, 1, 1 ],
                       [ 1, 0, 0 ]];
           
        document.write(minimumflip(mat, n));
          
         // This code is contributed by sanjoy_62.
</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++




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




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


Javascript




<script>
    // Javascript Program to find minimum flip
    // required to make Binary Matrix
    // symmetric along main diagonal
     
    // Return the minimum flip required
    // to make Binary Matrix symmetric
    // along main diagonal.
    function minimumflip(mat, n)
    {
        // Comparing elements across diagonal
        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));
     
    // This code is contributed by mukesh07.
</script>


Output : 
 

2

Time Complexity: O(N2)

Auxiliary Space: O(1), since no extra space has been taken.
 



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

Similar Reads