Horje
Program to check if a matrix is Binary matrix or not

Given a matrix, the task is to check if that matrix is a Binary Matrix. A Binary Matrix is a matrix in which all the elements are either 0 or 1. It is also called Logical Matrix, Boolean Matrix, Relation Matrix.

Examples: 

Input: 
{{1, 0, 1, 1},
{0, 1, 0, 1}
{1, 1, 1, 0}}
Output: Yes

Input:
{{1, 0, 1, 1},
{1, 2, 0, 1},
{0, 0, 1, 1}}
Output: No

Approach: Traverse the matrix and check if every element is either 0 or 1. If there is any element other than 0 and 1, print No else print Yes.

Below is the implementation of above approach: 

C++

<?php
// PHP code to check if a matrix
// is binary matrix or not.
 
$M = 3;
$N = 4;
 
// function to check if a matrix
// is binary matrix or not
function isBinaryMatrix($mat)
{
    global $M, $N;
    for ($i = 0; $i < $M; $i++)
    {
        for ($j = 0; $j < $N; $j++)
        {
            // Returns false if element
            // is other than 0 or 1.
            if (!($mat[$i][$j] == 0 ||
                  $mat[$i][$j] == 1))
                return false;
        }
    }
 
    // Returns true if all the
    // elements are either 0 or 1.
    return true;
}
 
// Driver code
$mat = array(array( 1, 0, 1, 1 ),
             array( 0, 1, 0, 1 ),
             array( 1, 1, 1, 0 ));
 
if (isBinaryMatrix($mat))
    echo "Yes";
else
    echo "No";
 
// This code is contributed by mits
?>

Javascript

<script>
 
// JAVA SCRIPT  code to check if a matrix
// is binary matrix or not.
 
 
    let M = 3;
   let N = 4;
 
    // function to check if a matrix is binary matrix
    // or not
    function isBinaryMatrix(mat)
    {
        for (let i = 0; i < M; i++) {
            for (let j = 0; j < N; j++) {
                // Returns false if element is other than 0 or 1.
                if (!(mat[i][j] == 0 || mat[i][j] == 1))
                    return false;
            }
        }
 
        // Returns true if all the elements
        // are either 0 or 1.
        return true;
    }
 
    // Driver code
    let mat = [[ 1, 0, 1, 1 ],
                        [ 0, 1, 0, 1 ],
                        [ 1, 1, 1, 0 ]];
 
        if (isBinaryMatrix(mat))
            document.write("Yes");
        else
            document.write("No");
         
// this code is  contributed by mohan pavan pulamolu
</script>

Output

Yes

Complexity Analysis:

  • Time complexity: O( M X N) 
  • Space Complexity: O(1)



Reffered: https://www.geeksforgeeks.org


Matrix

Related
Check if the given chessboard is valid or not Check if the given chessboard is valid or not
Check if a Matrix is Invertible Check if a Matrix is Invertible
Minimum changes needed to make a 3*3 matrix magic square Minimum changes needed to make a 3*3 matrix magic square
Sudo Placement[1.5] | Wolfish Sudo Placement[1.5] | Wolfish
Count of elements of an array present in every row of NxM matrix Count of elements of an array present in every row of NxM matrix

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
8