Programa para comprobar si una array es array binaria o no

Dada una array, la tarea es verificar si esa array es una array binaria . Una Array Binaria es una array en la que todos los elementos son 0 o 1. También se llama Array Lógica, Array Booleana, Array de Relación.
Ejemplos: 
 

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

Enfoque: recorra la array y verifique si cada elemento es 0 o 1. Si hay algún elemento que no sea 0 y 1, imprima No, de lo contrario, imprima Sí.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ code to check if a matrix
// is binary matrix or not.
#include <bits/stdc++.h>
using namespace std;
 
#define M 3
#define N 4
 
// function to check if a matrix
// is binary matrix or not
bool isBinaryMatrix(int mat[][N])
{
    for (int i = 0; i < M; i++) {
        for (int 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
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

// JAVA code to check if a matrix
// is binary matrix or not.
 
import java.io.*;
class GFG {
    static int M = 3;
    static int N = 4;
 
    // function to check if a matrix is binary matrix
    // or not
    static boolean isBinaryMatrix(int mat[][])
    {
        for (int i = 0; i < M; i++) {
            for (int 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
    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

# Python3 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
def isBinaryMatrix(mat):
    for i in range(M):
        for j in range(N):
            # Returns false if element
            # is other than 0 or 1.
            if ((mat[i][j] == 0 or mat[i][j] == 1)==False):
                return False;
 
    # Returns true if all the
    # elements are either 0 or 1.
    return True;
 
# Driver code
if __name__=='__main__':
    mat = [[ 1, 0, 1, 1 ],[0, 1, 0, 1 ],[ 1, 1, 1, 0 ]];
 
    if (isBinaryMatrix(mat)):
        print("Yes");
    else:
        print("No");
 
# This code is contributed by mits

C#

// C# code to check if a matrix
// is binary matrix or not.
 
using System;
class GFG {
    static int M = 3;
    static int N = 4;
 
    // function to check if a matrix is binary matrix
    // or not
    static bool isBinaryMatrix(int [,]mat)
    {
        for (int i = 0; i < M; i++) {
            for (int 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
    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");
    }
}
// This code is contributed by anuj_67.

PHP

<?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>
Producción: 

Yes

 

Complejidad temporal: O(MXN) 
Complejidad espacial: O(1)
 

Publicación traducida automáticamente

Artículo escrito por manish_chauhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *