Dada una array cuadrada NxN M[][] . La tarea es verificar si la array M es una array de división cero o no. Se dice que una array es una array de división por cero solo si los 2 o más resultados de la división mínima del producto del elemento por columnas por el producto del elemento por filas es cero .
Ejemplos:
Entrada: M[ ][ ] = [ [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] ]Salida: 1
Explicación: Consulte la imagen para obtener una explicación.Entrada: M[ ][ ] = [ [ 1, 2, 3 ],
[ 6, 8, 2 ],
[ 7, 8, 9 ] ]
Salida: 0
Enfoque: Este problema está basado en la implementación. Siga los pasos a continuación para resolver el problema dado.
- Tome una array M[ ][ ] y calcule el producto de los elementos por fila y por columna.
- Almacene el producto de cada fila y columna en la array R[ ] y C[ ] respectivamente.
- Ahora tome la división de piso del elemento de índice de la array C[] por la array R[] .
- Si obtenemos dos o más cero, entonces es una cuadrícula de división por cero y devuelve 1 ; de lo contrario, devuelve 0 .
- En caso de que el denominador tenga cero, entonces es un caso de error de división por cero que devuelve -1 .
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program for above approach #include <bits/stdc++.h> using namespace std; // Function to check zero division matrix int ZeroDiv(vector<vector<int> >& M, int& N) { // R and C array to store product vector<int> R, C; // Iterate through the matrix // to take product for (int i = 0; i < N; i++) { int r = 1; int c = 1; for (int j = 0; j < N; j++) { r *= M[i][j]; c *= M[j][i]; } // Appending product of each row // and column to R and C array. R.push_back(r); C.push_back(c); } // z to count number of zero int z = 0; // Try block to check zero division error try { for (int i = 0; i < N; i++) if (C[i] / R[i] == 0) z += 1; } // If zero division error occur return -1 catch (int x) { cout << x << "\n"; return -1; } // If z greater than equal to 2 return 1 if (z >= 2) { return 1; } // Else return 0 else { return 0; } } // Driver Code int main() { // # Matrix M vector<vector<int> > M = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // N length of matrix M int N = M.size(); // Call zerodiv function int x = ZeroDiv(M, N); // Print the answer cout << x << "\n"; } // This code is contributed by Taranpreet
Java
// JAVA program for above approach import java.util.*; class GFG { // Function to check zero division matrix public static int ZeroDiv(ArrayList<ArrayList<Integer> > M, int N) { // R and C array to store product ArrayList<Integer> R = new ArrayList<Integer>(); ArrayList<Integer> C = new ArrayList<Integer>(); // Iterate through the matrix // to take product for (int i = 0; i < N; i++) { int r = 1; int c = 1; for (int j = 0; j < N; j++) { r = r * M.get(i).get(j); c = c * M.get(i).get(j); } // Appending product of each row // and column to R and C array. R.add(r); C.add(c); } // z to count number of zero int z = 0; // Try block to check zero division error try { for (int i = 0; i < N; i++) if (C.get(i) % R.get(i) == 0) z = z + 1; } // If zero division error occur return -1 catch (Exception e) { System.out.println("Something went wrong" + e); return -1; } // If z greater than equal to 2 return 1 if (z >= 2) { return 1; } // Else return 0 else { return 0; } } // Driver Code public static void main(String[] args) { // # Matrix M ArrayList<ArrayList<Integer> > M = new ArrayList<ArrayList<Integer> >(); ArrayList<Integer> temp1 = new ArrayList<>(Arrays.asList(1, 2, 3)); ArrayList<Integer> temp2 = new ArrayList<>(Arrays.asList(4, 5, 6)); ArrayList<Integer> temp3 = new ArrayList<>(Arrays.asList(7, 8, 9)); M.add(temp1); M.add(temp2); M.add(temp3); // N length of matrix M int N = M.size(); // Call zerodiv function int x = ZeroDiv(M, N); // Print the answer System.out.println(x); } } // This code is contributed by Taranpreet
Python3
# Python program for above approach # Function to check zero division matrix def ZeroDiv(M, N): # R and C array to store product R = [] C = [] # Iterate through the matrix # to take product for i in range(N): r = 1 c = 1 for j in range(N): r *= M[i][j] c *= M[j][i] # Appending product of each row # and column to R and C array. R.append(r) C.append(c) # z to count number of zero z = 0 # Try block to check zero division error try: for i in range(N): if C[i]//R[i] == 0: z += 1 # If zero division error occur return -1 except: return -1 # If z greater than equal to 2 return 1 if z >= 2: return 1 # Else return 0 else: return 0 # Driver Code if __name__ == "__main__": # Matrix M M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # N length of matrix M N = len(M) # Call zerodiv function x = ZeroDiv(M, N) # Print the answer print(x)
C#
// C# program for above approach using System; using System.Collections.Generic; class GFG { // Function to check zero division matrix public static int ZeroDiv(List<List<int> > M, int N) { // R and C array to store product List<int> R = new List<int>(); List<int> C = new List<int>(); // Iterate through the matrix // to take product for (int i = 0; i < N; i++) { int r = 1; int c = 1; for (int j = 0; j < N; j++) { r = r * M[i][j]; c = c * M[i][j]; } // Appending product of each row // and column to R and C array. R.Add(r); C.Add(c); } // z to count number of zero int z = 0; // Try block to check zero division error try { for (int i = 0; i < N; i++) if (C[i] % R[i] == 0) z = z + 1; } // If zero division error occur return -1 catch (Exception e) { Console.WriteLine("Something went wrong" + e); return -1; } // If z greater than equal to 2 return 1 if (z >= 2) { return 1; } // Else return 0 else { return 0; } } // Driver Code public static void Main(string[] args) { // # Matrix M List<List<int> > M = new List<List<int> >(); List<int> temp1 = new List<int>(){1, 2, 3}; List<int> temp2 = new List<int>(){4, 5, 6}; List<int> temp3 = new List<int>(){7, 8, 9}; M.Add(temp1); M.Add(temp2); M.Add(temp3); // N length of matrix M int N = M.Count; // Call zerodiv function int x = ZeroDiv(M, N); // Print the answer Console.WriteLine(x); } } // This code is contributed by ukasp.
Javascript
<script> // JavaScript code for the above approach // Function to check zero division matrix function ZeroDiv(M, N) { // R and C array to store product let R = [] let C = [] // Iterate through the matrix // to take product for (let i = 0; i < N; i++) { r = 1 c = 1 for (let j = 0; j < N; j++) { r *= M[i][j] c *= M[j][i] } // Appending product of each row // and column to R and C array. R.push(r) C.push(c) } // z to count number of zero z = 0 // Try block to check zero division error try { for (let i = 0; i < N; i++) if (Math.floor(C[i] / R[i]) == 0) z += 1 } // If zero division error occur return -1 catch { return -1 } // If z greater than equal to 2 return 1 if (z >= 2) return 1 // Else return 0 else return 0 } // Driver Code // Matrix M let M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] // N length of matrix M let N = M.length // Call zerodiv function let x = ZeroDiv(M, N) // Print the answer document.write(x) // This code is contributed by Potta Lokesh </script>
1
Complejidad temporal: O(N*N)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por harshdeepmahajan88 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA