Array ( [0] => [1] => questions [2] => HackwithInfy-Previous-Questions [3] => Addition-of-two-matrixes )
Given two 2D sets of elements representing of M*N multidimensional array. You have to perform addition 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 1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
2 2 1 2 3 4 1 2 3 4
2 4 6 8
3 3 3 2 1 6 5 4 9 8 7 3 2 1 6 5 4 9 8 7
6 4 2 12 10 8 18 16 14
Login to see Discussion
Step-i) Run two loops in order to traverse through rows and colums
Step-ii) Add 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