Check if given matrix is Zero Division matrix
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++
#include <bits/stdc++.h>
using namespace std;
int ZeroDiv(vector<vector< int > >& M, int & N)
{
vector< int > R, C;
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];
}
R.push_back(r);
C.push_back(c);
}
int z = 0;
try {
for ( int i = 0; i < N; i++)
if (C[i] / R[i] == 0)
z += 1;
}
catch ( int x) {
cout << x << "\n" ;
return -1;
}
if (z >= 2) {
return 1;
}
else {
return 0;
}
}
int main()
{
vector<vector< int > > M
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int N = M.size();
int x = ZeroDiv(M, N);
cout << x << "\n" ;
}
|
Java
import java.util.*;
class GFG
{
public static int
ZeroDiv(ArrayList<ArrayList<Integer> > M, int N)
{
ArrayList<Integer> R = new ArrayList<Integer>();
ArrayList<Integer> C = new ArrayList<Integer>();
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);
}
R.add(r);
C.add(c);
}
int z = 0 ;
try {
for ( int i = 0 ; i < N; i++)
if (C.get(i) % R.get(i) == 0 )
z = z + 1 ;
}
catch (Exception e) {
System.out.println( "Something went wrong" + e);
return - 1 ;
}
if (z >= 2 ) {
return 1 ;
}
else {
return 0 ;
}
}
public static void main(String[] args)
{
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);
int N = M.size();
int x = ZeroDiv(M, N);
System.out.println(x);
}
}
|
Python3
def ZeroDiv(M, N):
R = []
C = []
for i in range (N):
r = 1
c = 1
for j in range (N):
r * = M[i][j]
c * = M[j][i]
R.append(r)
C.append(c)
z = 0
try :
for i in range (N):
if C[i] / / R[i] = = 0 :
z + = 1
except :
return - 1
if z > = 2 :
return 1
else :
return 0
if __name__ = = "__main__" :
M = [[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]]
N = len (M)
x = ZeroDiv(M, N)
print (x)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int
ZeroDiv(List<List< int > > M, int N)
{
List< int > R = new List< int >();
List< int > C = new List< int >();
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];
}
R.Add(r);
C.Add(c);
}
int z = 0;
try {
for ( int i = 0; i < N; i++)
if (C[i] % R[i] == 0)
z = z + 1;
}
catch (Exception e) {
Console.WriteLine( "Something went wrong" + e);
return -1;
}
if (z >= 2) {
return 1;
}
else {
return 0;
}
}
public static void Main( string [] args)
{
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);
int N = M.Count;
int x = ZeroDiv(M, N);
Console.WriteLine(x);
}
}
|
Javascript
<script>
function ZeroDiv(M, N) {
let R = []
let C = []
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]
}
R.push(r)
C.push(c)
}
z = 0
try {
for (let i = 0; i < N; i++)
if (Math.floor(C[i] / R[i]) == 0)
z += 1
}
catch {
return -1
}
if (z >= 2)
return 1
else
return 0
}
let M = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
let N = M.length
let x = ZeroDiv(M, N)
document.write(x)
</script>
|
Time Complexity: O(N*N)
Auxiliary Space: O(N)
Last Updated :
11 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...