Sum of matrix in which each element is absolute difference of its row and column numbers
Last Updated :
12 Sep, 2023
Given a positive integer n. Consider a matrix of n rows and n columns, in which each element contain absolute difference of its row number and numbers. The task is to calculate sum of each element of the matrix.
Examples :
Input : n = 2
Output : 2
Matrix formed with n = 2 with given constraint:
0 1
1 0
Sum of matrix = 2.
Input : n = 3
Output : 8
Matrix formed with n = 3 with given constraint:
0 1 2
1 0 1
2 1 0
Sum of matrix = 8.
Method 1 (Brute Force): Simply construct a matrix of n rows and n columns and initialize each cell with absolute difference of its corresponding row number and column number. Now, find the sum of each cell.
Below is the implementation of above idea :
C++
#include<bits/stdc++.h>
using namespace std;
int findSum( int n)
{
int arr[n][n];
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
arr[i][j] = abs (i - j);
int sum = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
sum += arr[i][j];
return sum;
}
int main()
{
int n = 3;
cout << findSum(n) << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int findSum( int n)
{
int [][]arr = new int [n][n];
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < n; j++)
arr[i][j] = Math.abs(i - j);
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < n; j++)
sum += arr[i][j];
return sum;
}
static public void main (String[] args)
{
int n = 3 ;
System.out.println(findSum(n));
}
}
|
Python3
def findSum(n):
arr = [[ 0 for x in range (n)]
for y in range (n)]
for i in range (n):
for j in range (n):
arr[i][j] = abs (i - j)
sum = 0
for i in range (n):
for j in range (n):
sum + = arr[i][j]
return sum
if __name__ = = "__main__" :
n = 3
print (findSum(n))
|
C#
using System;
public class GFG {
static int findSum( int n)
{
int [,]arr = new int [n, n];
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
arr[i,j ] = Math.Abs(i - j);
int sum = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
sum += arr[i, j];
return sum;
}
static public void Main(String[] args)
{
int n = 3;
Console.WriteLine(findSum(n));
}
}
|
PHP
<?php
function findSum( $n )
{
$arr = array ( array ());
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 0; $j < $n ; $j ++)
$arr [ $i ][ $j ] = abs ( $i - $j );
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 0; $j < $n ; $j ++)
$sum += $arr [ $i ][ $j ];
return $sum ;
}
$n = 3;
echo findSum( $n );
?>
|
Javascript
<script>
function findSum(n)
{
let arr= new Array(n);
for (let i = 0; i < n; i++)
{
arr[i] = new Array(n);
for (let j = 0; j < n; j++)
{
arr[i][j] = 0;
}
}
for (let i = 0; i < n; i++)
for (let j = 0; j < n; j++)
arr[i][j] = Math.abs(i - j);
let sum = 0;
for (let i = 0; i < n; i++)
for (let j = 0; j < n; j++)
sum += arr[i][j];
return sum;
}
let n = 3;
document.write(findSum(n));
</script>
|
Time Complexity: O(N2), as we are traversing the matrix using nested loops.
Auxiliary Space: O(N2), as we are using extra space for generating and storing the Matrix.
Method 2 (O(n)):
Consider n = 3, matrix formed will be:
0 1 2
1 0 1
2 1 0
Observe, the main diagonal is always 0 since all i are equal to j. The diagonal just above and just below will always be 1 because at each cell either i is 1 greater than j or j is 1 greater than i and so on.
Following the pattern we can see that the total sum of all the elements in the matrix will be, for each i from 0 to n, add i*(n-i)*2.
Below is the implementation of above idea :
C++
#include<bits/stdc++.h>
using namespace std;
int findSum( int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += i*(n-i);
return 2*sum;
}
int main()
{
int n = 3;
cout << findSum(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findSum( int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += i * (n - i);
return 2 * sum;
}
static public void main(String[] args)
{
int n = 3 ;
System.out.println(findSum(n));
}
}
|
C#
using System;
class GFG {
static int findSum( int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += i * (n - i);
return 2 * sum;
}
static public void Main(String[] args)
{
int n = 3;
Console.WriteLine(findSum(n));
}
}
|
Python3
def findSum(n):
sum = 0
for i in range (n):
sum + = i * (n - i)
return 2 * sum
n = 3
print (findSum(n))
|
PHP
<?php
function findSum( $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum += $i * ( $n - $i );
return 2 * $sum ;
}
$n = 3;
echo findSum( $n );
?>
|
Javascript
<script>
function findSum( n)
{
let sum = 0;
for (let i = 0; i < n; i++)
sum += i * (n - i);
return 2 * sum;
}
let n = 3;
document.write(findSum(n));
</script>
|
Time Complexity: O(N), as we are only using single loop to traverse.
Auxiliary Space: O(1), as we are not using any extra space.
Method 3 (Trick):
Consider n = 3, matrix formed will be:
0 1 2
1 0 1
2 1 0
So, sum = 1 + 1 + 1 + 1 + 2 + 2.
On Rearranging, 1 + 2 + 1 + 2 + 2 = 1 + 2 + 1 + 22.
So, in every case we can rearrange the sum of matrix so that the answer always will be sum of first n – 1 natural number and sum of square of first n – 1 natural number.
Sum of first n natural number = ((n)*(n + 1))/2.
Sum of first n natural number = ((n)*(n + 1)*(2*n + 1)/6.
Below is the implementation of above idea :
C++
#include<bits/stdc++.h>
using namespace std;
int findSum( int n)
{
n--;
int sum = 0;
sum += (n*(n+1))/2;
sum += (n*(n+1)*(2*n + 1))/6;
return sum;
}
int main()
{
int n = 3;
cout << findSum(n) << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int findSum( int n)
{
n--;
int sum = 0 ;
sum += (n * (n + 1 )) / 2 ;
sum += (n * (n + 1 ) * ( 2 * n + 1 )) / 6 ;
return sum;
}
static public void main (String[] args)
{
int n = 3 ;
System.out.println(findSum(n));
}
}
|
Python3
def findSum(n):
n - = 1
sum = 0
sum + = (n * (n + 1 )) / 2
sum + = (n * (n + 1 ) * ( 2 * n + 1 )) / 6
return int ( sum )
n = 3
print (findSum(n))
|
C#
using System;
public class GFG {
static int findSum( int n)
{
n--;
int sum = 0;
sum += (n * (n + 1)) / 2;
sum += (n * (n + 1) * (2 * n + 1)) / 6;
return sum;
}
static public void Main(String[] args)
{
int n = 3;
Console.WriteLine(findSum(n));
}
}
|
PHP
<?php
function findSum( $n )
{
$n --;
$sum = 0;
$sum += ( $n * ( $n + 1)) / 2;
$sum += ( $n * ( $n + 1) *
(2 * $n + 1)) / 6;
return $sum ;
}
$n = 3;
echo findSum( $n ) ;
?>
|
Javascript
<script>
function findSum( n)
{
n--;
let sum = 0;
sum += (n * (n + 1)) / 2;
sum += (n * (n + 1) * (2 * n + 1)) / 6;
return sum;
}
let n = 3;
document.write(findSum(n));
</script>
|
Time Complexity: O(1), as we are not using any loops.
Auxiliary Space: O(1), as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...