Table of Contents
Problem Statement
Given two matrices, we will write a function to check whether the two matrices are identical or not. That is, if all the elements in the respective positions of the two matrices are the same, then we say that they are identical.
Input Format
The first line containing four integer values r1, c1, r2, c2. Where r1 and c1 denote the number of rows and columns of the first matrix. And r2, c2 denotes the number of rows, columns of the second matrix.
Next r1 lines containing c1 integer values.
And next r2 lines containing c2 integer values.
Output Format
Print “Identical matrices
” if the matrices are identical otherwise print “Not identical matrices
“.
Constraints
- 1<=r1, c1, r2, c2<=5000.
- 1<= |m[i][j]| <=10^9 where m is the matrix and the position of element at ith row and jth column.
Example
4 4 4 4 1 4 7 9 1 6 7 6 6 3 7 12 4 4 4 4 1 4 7 9 1 6 7 6 6 3 7 12 4 4 4 4
Identical matrices
Explanation: In the above example, all the elements in the matrices are the same, so they are identical.
Let’s see how to check for every elements in the matrices by follow the below image. This is another example for better understanding.
Algorithm for Check if Two given Matrices are Identical
1. For each row in the two matrices, compare the elements in that row.
1.1 If same, move to the next row.
1.2 Else break and print “matrices are not identical”.
2. If the loop didn’t break, then the matrices are identical.
Implementation
C++ Program for Check if Two given Matrices are Identical
#include <bits/stdc++.h> using namespace std; int main() { int r1,c1,r2,c2; cin>>r1>>c1>>r2>>c2; int a[r1][c1]; int b[r2][c2]; for(int i=0;i<r1;i++) { for(int j=0;j<c1;j++) { cin>>a[i][j]; } } for(int i=0;i<r2;i++) { for(int j=0;j<c2;j++) { cin>>b[i][j]; } } if(c1!=c2 || r1!=r2) { cout<<"Not identical matrices"; } else { int flag=0; for(int i=0;i<r1;i++) { for(int j=0;j<c1;j++) { if(a[i][j]!=b[i][j]) { flag=1; break; } } } if(flag==1) { cout<<"Identical matrices"; } } return 0; }
Java Program for Check if Two given Matrices are Identical
import java.io.*; import java.util.Scanner; class sum { // Driver code public static void main(String[] args) { int r1,c1,r2,c2; Scanner inp = new Scanner(System.in); r1 = inp.nextInt(); c1 = inp.nextInt(); r2 = inp.nextInt(); c2 = inp.nextInt(); int a[][] = new int[r1][c1]; for(int i=0;i<r1;i++) { for(int j=0;j<c1;j++) { a[i][j]=inp.nextInt(); } } int b[][] = new int[r2][c2]; for(int i=0;i<r2;i++) { for(int j=0;j<c2;j++) { b[i][j]=inp.nextInt(); } } if(r1!=r2 && c1!=c2) { System.out.println("\nNot identical matrices"); } else { for(int i=0;i<r1;i++) { for(int j=0;j<c1;j++) { if(a[i][j]!=b[i][j]) { System.out.println("\nNot identical matrices"); return; } } } System.out.println("\nIdentical matrices"); } } }
2 2 2 2 1 2 3 4 1 2 3 4
Identical matrices
Complexity Analysis for Check if Two given Matrices are Identical
Time Complexity
O(n^2) where n is the maximum of r1 and c1. Here we simply run two loops first loop run r1 times and the second loop runs c1 times.
Space Complexity
O(1) because we don’t create any extra space for finding the result. Here we only declared r1*c1 size for taking input the first matrix and r2*c2 size for taking input the second matrix.