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++
#include <bits/stdc++.h>
using namespace std;
#define M 3
#define N 4
bool isBinaryMatrix( int mat[][N])
{
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
if (!(mat[i][j] == 0 || mat[i][j] == 1))
return false ;
}
}
return true ;
}
int main()
{
int mat[M][N] = { { 1, 0, 1, 1 },
{ 0, 1, 0, 1 },
{ 1, 1, 1, 0 } };
if (isBinaryMatrix(mat))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int M = 3 ;
static int N = 4 ;
static boolean isBinaryMatrix( int mat[][])
{
for ( int i = 0 ; i < M; i++) {
for ( int j = 0 ; j < N; j++) {
if (!(mat[i][j] == 0 || mat[i][j] == 1 ))
return false ;
}
}
return true ;
}
public static void main(String args[])
{
int mat[][] = { { 1 , 0 , 1 , 1 },
{ 0 , 1 , 0 , 1 },
{ 1 , 1 , 1 , 0 } };
if (isBinaryMatrix(mat))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
M = 3 ;
N = 4 ;
def isBinaryMatrix(mat):
for i in range (M):
for j in range (N):
if ((mat[i][j] = = 0 or mat[i][j] = = 1 ) = = False ):
return False ;
return True ;
if __name__ = = '__main__' :
mat = [[ 1 , 0 , 1 , 1 ],[ 0 , 1 , 0 , 1 ],[ 1 , 1 , 1 , 0 ]];
if (isBinaryMatrix(mat)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG {
static int M = 3;
static int N = 4;
static bool isBinaryMatrix( int [,]mat)
{
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
if (!(mat[i,j] == 0 || mat[i,j] == 1))
return false ;
}
}
return true ;
}
public static void Main()
{
int [,]mat = { { 1, 0, 1, 1 },
{ 0, 1, 0, 1 },
{ 1, 1, 1, 0 } };
if (isBinaryMatrix(mat))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
$M = 3;
$N = 4;
function isBinaryMatrix( $mat )
{
global $M , $N ;
for ( $i = 0; $i < $M ; $i ++)
{
for ( $j = 0; $j < $N ; $j ++)
{
if (!( $mat [ $i ][ $j ] == 0 ||
$mat [ $i ][ $j ] == 1))
return false;
}
}
return true;
}
$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" ;
?>
|
Javascript
<script>
let M = 3;
let N = 4;
function isBinaryMatrix(mat)
{
for (let i = 0; i < M; i++) {
for (let j = 0; j < N; j++) {
if (!(mat[i][j] == 0 || mat[i][j] == 1))
return false ;
}
}
return true ;
}
let mat = [[ 1, 0, 1, 1 ],
[ 0, 1, 0, 1 ],
[ 1, 1, 1, 0 ]];
if (isBinaryMatrix(mat))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Complexity Analysis:
- Time complexity: O( M X N)
- Space Complexity: O(1)
Last Updated :
01 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...