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





Addition of two matrixes

LEVEL:Beginner

Description

Given two 2D sets of elements representing of M*N multidimensional array. You have to perform addition 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
1 2 3
4 5 6
7 8 9
Output
2 4 6
8 10 12
14 16 18
Example 2:

Input
2 2
1 2 
3 4
1 2
3 4
Output
2 4
6 8
Example 3:

Input
3 3
3 2 1
6 5 4
9 8 7
3 2 1
6 5 4
9 8 7
Output
6 4 2
12 10 8
18 16 14

oops

Login to see Discussion




Approach


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.

oops

Login to see Solution