## The challenge

Write a function that accepts two square matrices (`N x N` two dimensional arrays), and return the sum of the two. Both matrices being passed into the function will be of size `N x N` (square), containing only integers.

How to sum two matrices:

Take each cell `[n][m]` from the first matrix, and add it with the same `[n][m]` cell from the second matrix. This will be cell `[n][m]` of the solution matrix.

Visualization:

 ``````1 2 3 `````` ``````|1 2 3| |2 2 1| |1+2 2+2 3+1| |3 4 4| |3 2 1| + |3 2 3| = |3+3 2+2 1+3| = |6 4 4| |1 1 1| |1 1 3| |1+1 1+1 1+3| |2 2 4| ``````

### Example

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 `````` ``````matrixAddition( [ [1, 2, 3], [3, 2, 1], [1, 1, 1] ], // + [ [2, 2, 1], [3, 2, 3], [1, 1, 3] ] ) // returns: [ [3, 4, 4], [6, 4, 4], [2, 2, 4] ] ``````

## The solution in Java code

Option 1:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 `````` ``````public class Solution { public static int[][] matrixAddition(int[][] a, int[][] b) { int matLen = a.length; int[][] resultMatrix = new int[matLen][matLen]; for(int i = 0; i < matLen; i++) { for(int j = 0; j < matLen; j++) { resultMatrix[i][j] = a[i][j] + b[i][j]; } } return resultMatrix; } } ``````

Option 2:

 ``````1 2 3 4 5 6 7 8 `````` ``````import java.util.stream.IntStream; public class Solution { public static int[][] matrixAddition(int[][] a, int[][] b) { IntStream.range(0, a.length * a.length) .forEach(n -> a[n / a.length][n % a.length] += b[n / a.length][n % a.length]); return a; } } ``````

Option 3:

 ``````1 2 3 4 5 6 7 8 9 `````` ``````public class Solution { public static int[][] matrixAddition(int[][] a, int[][] b) { for(int row=0;row

## Test cases to validate our solution

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 `````` ``````import org.junit.Test; import static org.junit.Assert.assertArrayEquals; public class MatrixAdditionTest { @Test public void sampleTest() { int[][] expected = new int[][] { { 3, 4, 4 }, { 6, 4, 4 }, { 2, 2, 4 } }; assertArrayEquals(expected, Solution.matrixAddition( new int[][] { { 1, 2, 3 }, { 3, 2, 1 }, { 1, 1, 1 } }, new int[][] { { 2, 2, 1 }, { 3, 2, 3 }, { 1, 1, 3 } })); expected = new int[][] { { 3 } }; assertArrayEquals(expected, Solution.matrixAddition( new int[][] { { 1 } }, new int[][] { { 2 } })); expected = new int[][] { { 3, 5 }, { 3, 5 } }; assertArrayEquals(expected, Solution.matrixAddition( new int[][] { { 1, 2 }, { 1, 2 } }, new int[][] { { 2, 3 }, { 2, 3 } })); } } ``````