![]() |
VOOZH | about |
Given a matrix mat[][], the task is to find the sum of all the elements of the matrix.
Examples:
Input: mat[][] = {{1, 2, 3}, {4, 5, 6}}
Output: 21
Explanation: Here sum of all element = 1 + 2 + 3 + 4 + 5 + 6 = 21Input: mat[][] = {{4, 5, 3, 2}, {9, 5, 6, 2}, {1, 5, 3, 5}}
Output: 50
Explanation: Here sum of all element = 4 + 5 + 3 + 2 + 9 + 5 + 6 + 2 + 1 + 5 + 3 + 5 = 50
The idea is to traverse every element of the matrix and keep adding them to a running sum.. This direct approach works efficiently as we must visit all elements anyway to compute the sum.
50