Array ( [0] => [1] => questions [2] => Arrays [3] => Subtraction-of-two-matrixes )
Given two 2D sets of elements representing of M*N multidimensional array. You have to perform subtraction operation between the two arrays.
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.
Print the resultant 2D array.
3 3 1 2 3 4 5 6 7 8 9 3 2 1 6 5 4 9 8 7
-2 0 2 -2 0 2 -2 0 2
2 2 1 2 3 4 1 2 3 4
0 0 0 0
3 3 3 2 1 6 5 4 9 8 7 1 2 3 4 5 6 7 8 9
2 0 -2 2 0 -2 2 0 -2
Login to see Discussion
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.
Login to see Solution