Sum of Matrix where each element is sum of row and column number
Last Updated :
21 Mar, 2023
Given two numbers M and N denoting the number of rows and columns of a matrix A[] where A[i][j] is the sum of i and j (indices follow 1 based indexing), the task is to find the sum of elements of the matrix.
Examples:
Input: M = 3, N = 3
Output: 36
Explanation: A[]: {{2, 3, 4}, {3, 4, 5}, {4, 5, 6}}. Sum of matrix: 36.
Input: M = 3, N = 4
Output: 54
Explanation: A[]: {{2, 3, 4, 5}, {3, 4, 5, 6}, {4, 5, 6, 7}}, Sum of matrix: 54
Naive Approach: To solve the problem follow the below idea:
Create a matrix of size M x N. While creating matrix make element at position (i, j) equal to i + j, where i and j are indices
(1 based indexing) of row and column of matrix. At last traverse on the matrix and return the sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int summation( int M, int N)
{
int matrix[M][N], sum = 0;
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
matrix[i][j] = (i + 1) + (j + 1);
sum += matrix[i][j];
}
}
return sum;
}
int main()
{
int M = 3, N = 4;
cout << summation(M, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int summation( int M, int N)
{
int matrix[][] = new int [M][N];
int sum = 0 ;
for ( int i = 0 ; i < M; i++) {
for ( int j = 0 ; j < N; j++) {
matrix[i][j] = (i + 1 ) + (j + 1 );
sum += matrix[i][j];
}
}
return sum;
}
public static void main(String[] args)
{
int M = 3 , N = 4 ;
System.out.print(summation(M, N));
}
}
|
Python3
def summation(M, N):
sum = 0
rows, cols = ( 5 , 5 )
matrix = [[ 0 ] * cols] * rows
for i in range ( 0 , M):
for j in range ( 0 , N):
matrix[i][j] = (i + 1 ) + (j + 1 )
sum + = matrix[i][j]
return sum
M = 3
N = 4
print (summation(M, N))
|
C#
using System;
public class GFG {
public static int summation( int M, int N)
{
int [,]matrix = new int [M,N];
int sum = 0;
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
matrix[i,j] = (i + 1) + (j + 1);
sum += matrix[i,j];
}
}
return sum;
}
public static void Main( string [] args)
{
int M = 3, N = 4;
Console.WriteLine(summation(M, N));
}
}
|
Javascript
<script>
function summation(M, N)
{
let matrix = [];
let sum =0;
for (let i=0;i<M;i++)
{matrix[i] = [];
for (let j=0;j<N;j++){
matrix[i][j] = 0
}
}
for (let i = 0; i < M; i++) {
for (let j = 0; j < N; j++) {
matrix[i][j] = (i + 1) + (j + 1);
sum += matrix[i][j];
}
}
return sum;
}
let M = 3;
let N = 4;
console.log(summation(M, N));
</script>
|
Time Complexity: O(M*N)
Auxiliary Space: O(M*N)
Efficient Approach: The approach to this problem is based on the following observation.
The elements of the matrix are repeating certain number of times.
If observed carefully, you can see an element with value (i + j) repeats min(i+j-1, N+M – (i+j-1)) times. and the sum of elements lies in the range [2, N+M]. So traverse in the range and find the repetition and find the sum of the matrix.
Illustration:
let M = 3 and N = 4 then matrix will be: {{2, 3, 4, 5}, {3, 4, 5, 6}, {4, 5, 6, 7}}
Occurrence of elements in matrix:
2 -> 1 time
3 -> 2 times
4 -> 3 times
5 -> 3 times
6 -> 2 times
7 -> 1 time
Total Summation = 2*1 + 3*2 + 4*3 + 5*3 + 6*2 + 7*1 = 54
Follow the steps mentioned below to implement the idea:
- Create a variable sum = 0 and start = M+N.
- Traverse in a loop from i = 1 to M.
- If the current index is greater than or equal to N, increment the sum by (start * N). Otherwise, increment the sum by (start * i).
- For each iteration decrement start.
- Traverse in a loop from i = N – 1 to 0
- If the current index is greater than or equal to M, increment the sum by (start * M). Otherwise, increment the sum by (start * i).
- For each iteration decrement start
- Return the sum as the required answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int summation( int M, int N)
{
int sum = 0, start = M + N;
for ( int i = 1; i <= M; i++) {
if (i >= N) {
sum += start * N;
}
else {
sum += start * i;
}
start--;
}
for ( int i = N - 1; i >= 1; i--) {
if (i >= M) {
sum += start * M;
}
else {
sum += start * i;
}
start--;
}
return sum;
}
int main()
{
int M = 3, N = 4;
cout << summation(M, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static int summation( int M, int N)
{
int sum = 0 , start = M + N;
for ( int i = 1 ; i <= M; i++) {
if (i >= N) {
sum += start * N;
}
else {
sum += start * i;
}
start--;
}
for ( int i = N - 1 ; i >= 1 ; i--) {
if (i >= M) {
sum += start * M;
}
else {
sum += start * i;
}
start--;
}
return sum;
}
public static void main(String[] args)
{
int M = 3 , N = 4 ;
System.out.println(summation(M, N));
}
}
|
Python3
def summation(M, N) :
sum = 0 ; start = M + N;
for i in range ( 1 ,M + 1 ) :
if (i > = N) :
sum + = start * N
else :
sum + = start * i
start - = 1
for i in range (N - 1 , 0 , - 1 ) :
if (i > = M) :
sum + = start * M
else :
sum + = start * i
start - = 1
return sum ;
if __name__ = = "__main__" :
M , N = 3 , 4 ;
print (summation(M, N))
|
C#
using System;
class GFG
{
public static int summation( int M, int N)
{
int sum = 0, start = M + N;
for ( int i = 1; i <= M; i++) {
if (i >= N) {
sum += start * N;
}
else {
sum += start * i;
}
start--;
}
for ( int i = N - 1; i >= 1; i--) {
if (i >= M) {
sum += start * M;
}
else {
sum += start * i;
}
start--;
}
return sum;
}
public static void Main()
{
int M = 3, N = 4;
Console.WriteLine(summation(M, N));
}
}
|
Javascript
<script>
function summation(M, N)
{
let sum = 0, start = M + N, i;
for (i = 1; i <= M; i++) {
if (i >= N) {
sum += start * N;
}
else {
sum += start * i;
}
start--;
}
for (i = N - 1; i >= 1; i--) {
if (i >= M) {
sum += start * M;
}
else {
sum += start * i;
}
start--;
}
return sum;
}
let M = 3, N = 4;
document.write(summation(M, N));
</script>
|
Time Complexity: O(M+N)
Auxiliary Space: O(1)
Another Efficient Approach( Time Optimization ): we can do following observation to find sum of all matrix elements in O(1) time .
Steps were to follow this problem:
- We can see that sum of first row is sum of first (n+1) natural number -1.First row sum = (n+1)*(n+2) /2 -1 .
- We can also observe that sum of second row is equal to sum of first row + n .and sum of third row equal to sum of second row + n. and so on for all next rows.
- So , our temp_sum equal to m*sum of first row . because there is atleast ‘sum of first row’ in every row.
- And sum of A.P series we can find by formula .
- SO , our total sum will be sum of temp_sum and sum of A.P series.
- Finally return total sum .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int summation( int M, int N)
{ int N1 = N+1;
int first_row_sum = (N1*(N1+1))/2-1;
int total_sum = M * first_row_sum ;
int a = N;
int d = N;
int n = M-1;
int sum_of_AP = (n*(2*a + (n-1)*d)) /2 ;
total_sum += sum_of_AP;
return total_sum;
}
int main()
{
int M = 3, N = 4;
cout << summation(M, N);
return 0;
}
|
Python3
def summation(M, N):
N1 = N + 1
first_row_sum = (N1 * (N1 + 1 )) / / 2 - 1
total_sum = M * first_row_sum
a = N
d = N
n = M - 1
sum_of_AP = (n * ( 2 * a + (n - 1 ) * d)) / / 2
total_sum + = sum_of_AP
return total_sum
if __name__ = = '__main__' :
M = 3
N = 4
print (summation(M, N))
|
Javascript
function summation(M, N)
{
let N1 = N + 1;
let first_row_sum = (N1 * (N1 + 1)) / 2 - 1;
let total_sum = M * first_row_sum;
let a = N;
let d = N;
let n = M - 1;
let sum_of_AP = (n * (2 * a + (n - 1) * d)) / 2;
total_sum += sum_of_AP;
return total_sum;
}
let M = 3,
N = 4;
console.log(summation(M, N));
|
C#
using System;
public class GFG{
public static int Summation( int M, int N)
{
int N1 = N + 1;
int first_row_sum = (N1 * (N1 + 1)) / 2 - 1;
int total_sum = M * first_row_sum;
int a = N;
int d = N;
int n = M - 1;
int sum_of_AP = (n * (2 * a + (n - 1) * d)) / 2;
total_sum += sum_of_AP;
return total_sum;
}
public static void Main( string [] args)
{
int M = 3, N = 4;
Console.WriteLine(Summation(M, N));
}
}
|
Java
public class GFG {
public static int summation( int M, int N) {
int N1 = N + 1 ;
int first_row_sum = (N1 * (N1 + 1 )) / 2 - 1 ;
int total_sum = M * first_row_sum;
int a = N;
int d = N;
int n = M - 1 ;
int sum_of_AP = (n * ( 2 * a + (n - 1 ) * d)) / 2 ;
total_sum += sum_of_AP;
return total_sum;
}
public static void main(String[] args) {
int M = 3 , N = 4 ;
System.out.println(summation(M, N));
}
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...