Array ( [0] => [1] => questions [2] => Popular-Algorithms [3] => Subtraction-of-two-matrixes ) Arrays | Subtraction of two matrixes | THE INQUISITIVE





Subtraction of two matrixes

LEVEL:Beginner

Description

Given two 2D sets of elements representing of M*N multidimensional array. You have to perform subtraction operation between the two arrays.

Input Format

First line contains two integers M and N. Next M lines contains N integers separated by spaces representing matrix1. Next M lines contains N integers separated by spaces representing matrix2.

Output Format

Print the resultant 2D array.


Example 1:

Input
3 3
1 2 3
4 5 6
7 8 9
3 2 1
6 5 4
9 8 7
Output
-2 0 2
-2 0 2
-2 0 2
Example 2:

Input
2 2
1 2 
3 4
1 2
3 4
Output
0 0
0 0
Example 3:

Input
3 3
3 2 1
6 5 4
9 8 7
1 2 3
4 5 6
7 8 9
Output
2 0 -2
2 0 -2
2 0 -2

oops

Login to see Discussion




Approach


Step-i) Run two loops in order to traverse through rows and colums
Step-ii) Subtract the respective elements of matrix
Step-iii) Store it in other matrix of same size (Here it is stored in String just for the simplicity of printing in main method)
Step-iv) Continue Step ii and iii till you completely traverse both the matrices

Time Complexity:- O(M*N) for a matrix of size M * N
usually it is termed as O(N^2)
Space Complexity:- O(M*N) usually termed as O(N^2)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution