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
Approach: This can be solved using the following idea:
Traverse through the whole matrix and add the value of the element with the result.
Follow the steps mentioned below to solve the problem:
- Initialize the variable sum = 0 to store the sum of the matrix.
- Run a loop to traverse each row of the matrix.
- Use a nested loop to traverse the columns of the matrix for each row.
- Add each element to the variable sum.
- Use a nested loop to traverse the columns of the matrix for each row.
- Return the sum as the required answer.
Pseudo Code:
sum = 0
for i = 0 to N-1:
for j = 0 to M-1:
sum = sum + mat[i][j]
return sum
Below is the implementation of the above approach:
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std;
// Function to find sum of all elements of matrix int sumOfMatrix( int N, int M, vector<vector< int > > mat)
{ // Initialise sum = 0 to store sum
// of each element
int sum = 0;
// 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 } };
int N = mat.size();
int M = mat[0].size();
// Function call
cout << sumOfMatrix(N, M, mat) << endl;
return 0;
} |
// Java program for the above approach import java.io.*;
class GFG {
// Function to find sum of all elements of matrix public static int sumOfMatrix( int N, int M, int mat[][])
{ // Initialise sum = 0 to store sum
// of each element
int sum = 0 ;
// 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 }
};
int N = mat.length;
int M = mat[ 0 ].length;
// Function call
System.out.println(sumOfMatrix(N, M, mat));
} } // This code is contributed by Pushpesh Raj |
# Python code for the above approach # Function to find sum of all elements of matrix def sumOfMatrix(N, M, mat):
# Initialize sum = 0 to store sum
# of each element
Sum = 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
Sum + = mat[i][j]
# Return sum of matrix
return Sum
mat = [[ 4 , 5 , 3 , 2 ],
[ 9 , 5 , 6 , 2 ],
[ 1 , 5 , 3 , 5 ]]
N = len (mat)
M = len (mat[ 0 ])
# Function call print (sumOfMatrix(N, M, mat))
# This code is contributed by lokesh |
// C# program for the above approach using System;
class GFG {
// Function to find sum of all elements of matrix
static int sumOfMatrix( int N, int M, int [, ] mat)
{
// Initialise sum = 0 to store sum
// of each element
int sum = 0;
// 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 } };
int N = mat.GetLength(0);
int M = mat.GetLength(1);
// Function call
Console.WriteLine(sumOfMatrix(N, M, mat));
}
} // This code is contributed by Samim Hossain Mondal. |
// JS code to implement the approach // Function to find sum of all elements of matrix function sumOfMatrix( N, M, mat)
{ // Initialise sum = 0 to store sum
// of each element
let sum = 0;
// 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 ] ];
let N = mat.length;
let M = mat[0].length;
// Function call
console.log(sumOfMatrix(N, M, mat));
// This code is contributed by ksam24000
|
50
Time Complexity: O(N * M), where N is the number of rows and M is the number of columns in the given matrix
Auxiliary Space: O(1)