Skip to content
Related Articles

Related Articles

Check if given matrix is Zero Division matrix

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 11 Feb, 2022

Given an NxN square matrix M[][]. The task is to check whether the matrix M is a zero division matrix or not. A matrix is said to be a zero division matrix only if the 2 or more results of floor division of the product of column-wise element to the product of row-wise element is zero.

Examples:

Input: M[ ][ ] = [ [ 1, 2, 3 ], 
                           [ 4, 5, 6 ], 
                          [ 7, 8, 9 ] ]

Output: 1
Explanation: Refer to image for explanation.

Input: M[ ][ ] = [ [ 1, 2, 3 ],       
                           [ 6, 8, 2 ], 
                           [ 7, 8, 9 ] ]
Output: 0

 

Approach: This problem is implementation-based. Follow the steps below to solve the given problem.

  • Take a matrix M[ ][ ] and calculate the product of row-wise and column-wise elements.
  • Store product of each row and column in R[ ] and C[ ] array respectively.
  • Now take floor division of index-wise element of C[ ] array by R[ ] array.
  • If we get two or more zero then it is zero division grid and return 1 else return 0.
  • In case the denominator has zero then it is case of zero division error return -1.

Below is the implementation of the above approach.

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>
Output
1

Time Complexity: O(N*N)
Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!