2D Matrix Sum

You are given a 2D matrix of size N × M.

Your task is to compute the sum of all elements in the matrix.

Example 1:

Input: matrix = [ [1, 2], [3, 4] ]

Output: 10

Explanation:

Sum of all elements = 1 + 2 + 3 + 4 = 10.

Example 2:

Input: matrix = [ [1, 2, 3], [4, 5, -6] ]

Output: 9

Explanation:

Sum of all elements = 1 + 2 + 3 + 4 + 5 - 6 = 9.

Now Your Turn!

Pick the correct output for the given input

Input: matrix = [ [-1, -2 , -3] ]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= N, M <= 50
  • -100 <= matrix[i][j] <= 100
0
class Solution {
public:
int matrixSum(int** matrix, int n, int m) {
//your code goes here
}
};
Test Case

Input:

Matrix