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
Try It Yourself
Using Matrix Traversal – O(n * m) Time and O(1) Space
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.
- Initialize a variable to store the total sum
- Traverse each row and column of the matrix using nested loops
- Add every element to the sum while traversing
#include <bits/stdc++.h>
using namespace std;
// Function to find sum of all elements of matrix
int sumOfMatrix(vector<vector<int> > &mat)
{
// Initialise sum = 0 to store sum
// of each element
int sum = 0;
int n = mat.size();
int m = mat[0].size();
// Traverse in each row
for (int i = 0; i < n; i++) {
// Traverse in column of that row
for (int j = 0; j < m; j++) {
// Add element in variable sum
sum += mat[i][j];
}
}
// Return sum of matrix
return sum;
}
// Driver Code
int main()
{
// Input Data
vector<vector<int> > mat = { { 4, 5, 3, 2 },
{ 9, 5, 6, 2 },
{ 1, 5, 3, 5 } };
// Function call
cout << sumOfMatrix(mat) << endl;
return 0;
}
import java.util.*;
class GFG {
// Function to find sum of all elements of matrix
static int sumOfMatrix(int[][] mat)
{
// Initialise sum = 0 to store sum
// of each element
int sum = 0;
int n = mat.length;
int m = mat[0].length;
// Traverse in each row
for (int i = 0; i < n; i++) {
// Traverse in column of that row
for (int j = 0; j < m; j++) {
// Add element in variable sum
sum += mat[i][j];
}
}
// Return sum of matrix
return sum;
}
// Driver Code
public static void main(String[] args)
{
// Input Data
int[][] mat = {
{4, 5, 3, 2},
{9, 5, 6, 2},
{1, 5, 3, 5}
};
// Function call
System.out.println(sumOfMatrix(mat));
}
}
# Function to find sum of all elements of matrix
def sumOfMatrix(mat):
# Initialise sum = 0 to store sum
# of each element
total = 0
n = len(mat)
m = len(mat[0])
# Traverse in each row
for i in range(n):
# Traverse in column of that row
for j in range(m):
# Add element in variable sum
total += mat[i][j]
# Return sum of matrix
return total
# Driver Code
# Input Data
mat = [
[4, 5, 3, 2],
[9, 5, 6, 2],
[1, 5, 3, 5]
]
# Function call
print(sumOfMatrix(mat))
using System;
class GFG {
// Function to find sum of all elements of matrix
static int sumOfMatrix(int[,] mat)
{
// Initialise sum = 0 to store sum
// of each element
int sum = 0;
int n = mat.GetLength(0);
int m = mat.GetLength(1);
// Traverse in each row
for (int i = 0; i < n; i++) {
// Traverse in column of that row
for (int j = 0; j < m; j++) {
// Add element in variable sum
sum += mat[i, j];
}
}
// Return sum of matrix
return sum;
}
// Driver Code
public static void Main()
{
// Input Data
int[,] mat = {
{4, 5, 3, 2},
{9, 5, 6, 2},
{1, 5, 3, 5}
};
// Function call
Console.WriteLine(sumOfMatrix(mat));
}
}
// Function to find sum of all elements of matrix
function sumOfMatrix(mat)
{
// Initialise sum = 0 to store sum
// of each element
let sum = 0;
let n = mat.length;
let m = mat[0].length;
// Traverse in each row
for (let i = 0; i < n; i++) {
// Traverse in column of that row
for (let j = 0; j < m; j++) {
// Add element in variable sum
sum += mat[i][j];
}
}
// Return sum of matrix
return sum;
}
// Driver Code
// Input Data
let mat = [
[4, 5, 3, 2],
[9, 5, 6, 2],
[1, 5, 3, 5]
];
// Function call
console.log(sumOfMatrix(mat));
Output
50