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++
// C++ program to find sum of matrix in which each
// element is absolute difference of its corresponding
// row and column number row.
#include<bits/stdc++.h>
using namespace std;
// Return the sum of matrix in which each element
// is absolute difference of its corresponding row
// and column number row
int findSum(int n)
{
// Generate matrix
int arr[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
arr[i][j] = abs(i - j);
// Compute sum
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
sum += arr[i][j];
return sum;
}
// Driven Program
int main()
{
int n = 3;
cout << findSum(n) << endl;
return 0;
}
Java
// Java program to find sum of matrix
// in which each element is absolute
// difference of its corresponding
// row and column number row.
import java.io.*;
public class GFG {
// Return the sum of matrix in which
// each element is absolute difference
// of its corresponding row and column
// number row
static int findSum(int n)
{
// Generate matrix
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);
// Compute sum
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
sum += arr[i][j];
return sum;
}
// Driver Code
static public void main (String[] args)
{
int n = 3;
System.out.println(findSum(n));
}
}
// This code is contributed by vt_m.
Python3
# Python3 program to find sum of matrix
# in which each element is absolute
# difference of its corresponding
# row and column number row.
# Return the sum of matrix in which each
# element is absolute difference of its
# corresponding row and column number row
def findSum(n):
# Generate matrix
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)
# Compute sum
sum = 0
for i in range (n):
for j in range(n):
sum += arr[i][j]
return sum
# Driver Code
if __name__ == "__main__":
n = 3
print (findSum(n))
# This code is contributed by ita_c
C#
// C# program to find sum of matrix
// in which each element is absolute
// difference of its corresponding
// row and column number row.
using System;
public class GFG {
// Return the sum of matrix in which
// each element is absolute difference
// of its corresponding row and column
// number row
static int findSum(int n)
{
// Generate matrix
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);
// Compute sum
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
sum += arr[i, j];
return sum;
}
// Driver Code
static public void Main(String[] args)
{
int n = 3;
Console.WriteLine(findSum(n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to find sum of
// matrix in which each element
// is absolute difference of
// its corresponding row and
// column number row.
// Return the sum of matrix
// in which each element
// is absolute difference
// of its corresponding row
// and column number row
function findSum( $n)
{
// Generate matrix
$arr =array(array());
for($i = 0; $i < $n; $i++)
for($j = 0; $j < $n; $j++)
$arr[$i][$j] = abs($i - $j);
// Compute sum
$sum = 0;
for($i = 0; $i < $n; $i++)
for ($j = 0; $j < $n; $j++)
$sum += $arr[$i][$j];
return $sum;
}
// Driver Code
$n = 3;
echo findSum($n);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript program to find sum of matrix
// in which each element is absolute
// difference of its corresponding
// row and column number row.
// Return the sum of matrix in which
// each element is absolute difference
// of its corresponding row and column
// number row
function findSum(n)
{
// Generate matrix
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);
// Compute sum
let sum = 0;
for (let i = 0; i < n; i++)
for (let j = 0; j < n; j++)
sum += arr[i][j];
return sum;
}
// Driver Code
let n = 3;
document.write(findSum(n));
// This code is contributed by avanitrachhadiya2155
</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++
// C++ program to find sum of matrix in which
// each element is absolute difference of its
// corresponding row and column number row.
#include<bits/stdc++.h>
using namespace std;
// Return the sum of matrix in which each
// element is absolute difference of its
// corresponding row and column number row
int findSum(int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += i*(n-i);
return 2*sum;
}
// Driven Program
int main()
{
int n = 3;
cout << findSum(n) << endl;
return 0;
}
Java
// Java program to find sum of matrix in which
// each element is absolute difference of its
// corresponding row and column number row.
import java.io.*;
class GFG {
// Return the sum of matrix in which each
// element is absolute difference of its
// corresponding row and column number row
static int findSum(int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += i * (n - i);
return 2 * sum;
}
// Driver Code
static public void main(String[] args)
{
int n = 3;
System.out.println(findSum(n));
}
}
// This code is contributed by vt_m.
C#
// C# program to find sum of matrix in which
// each element is absolute difference of its
// corresponding row and column number row.
using System;
class GFG {
// Return the sum of matrix in which each
// element is absolute difference of its
// corresponding row and column number row
static int findSum(int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += i * (n - i);
return 2 * sum;
}
// Driver Code
static public void Main(String[] args)
{
int n = 3;
Console.WriteLine(findSum(n));
}
}
// This code is contributed by vt_m.
Python3
# Python 3 program to find sum
# of matrix in which each element
# is absolute difference of its
# corresponding row and column
# number row.
# Return the sum of matrix in
# which each element is absolute
# difference of its corresponding
# row and column number row
def findSum(n):
sum = 0
for i in range(n):
sum += i * (n - i)
return 2 * sum
# Driver code
n = 3
print(findSum(n))
# This code is contributed by Shrikant13
PHP
<?php
// PHP program to find sum of matrix in which
// each element is absolute difference of its
// corresponding row and column number row.
// Return the sum of matrix in which each
// element is absolute difference of its
// corresponding row and column number row
function findSum($n)
{
$sum = 0;
for ( $i = 0; $i < $n; $i++)
$sum += $i * ($n - $i);
return 2 * $sum;
}
// Driver Code
$n = 3;
echo findSum($n);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Java script program to find sum of matrix in which
// each element is absolute difference of its
// corresponding row and column number row.
// Return the sum of matrix in which each
// element is absolute difference of its
// corresponding row and column number row
function findSum( n)
{
let sum = 0;
for (let i = 0; i < n; i++)
sum += i * (n - i);
return 2 * sum;
}
// Driver Code
let n = 3;
document.write(findSum(n));
// This code is contributed by mohan pavan
</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++
// C++ program to find sum of matrix in which
// each element is absolute difference of its
// corresponding row and column number row.
#include<bits/stdc++.h>
using namespace std;
// Return the sum of matrix in which each element
// is absolute difference of its corresponding
// row and column number row
int findSum(int n)
{
n--;
int sum = 0;
sum += (n*(n+1))/2;
sum += (n*(n+1)*(2*n + 1))/6;
return sum;
}
// Driven Program
int main()
{
int n = 3;
cout << findSum(n) << endl;
return 0;
}
Java
// Java program to find sum of matrix in which
// each element is absolute difference of its
// corresponding row and column number row.
import java.io.*;
public class GFG {
// Return the sum of matrix in which each element
// is absolute difference of its corresponding
// row and column number row
static int findSum(int n)
{
n--;
int sum = 0;
sum += (n * (n + 1)) / 2;
sum += (n * (n + 1) * (2 * n + 1)) / 6;
return sum;
}
// Driver Code
static public void main (String[] args)
{
int n = 3;
System.out.println(findSum(n));
}
}
// This code is contributed by vt_m.
Python3
# Python 3 program to find sum of matrix
# in which each element is absolute
# difference of its corresponding row
# and column number row.
# Return the sum of matrix in which
# each element is absolute difference
# of its corresponding row and column
# number row
def findSum(n):
n -= 1
sum = 0
sum += (n * (n + 1)) / 2
sum += (n * (n + 1) * (2 * n + 1)) / 6
return int(sum)
# Driver Code
n = 3
print(findSum(n))
# This code contributed by Rajput-Ji
C#
// C# program to find sum of matrix in which
// each element is absolute difference of its
// corresponding row and column number row.
using System;
public class GFG {
// Return the sum of matrix in which each element
// is absolute difference of its corresponding
// row and column number row
static int findSum(int n)
{
n--;
int sum = 0;
sum += (n * (n + 1)) / 2;
sum += (n * (n + 1) * (2 * n + 1)) / 6;
return sum;
}
// Driver Code
static public void Main(String[] args)
{
int n = 3;
Console.WriteLine(findSum(n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to find sum of
// matrix in which each element
// is absolute difference of its
// corresponding row and column
// number row.
// Return the sum of matrix in
// which each element is absolute
// difference of its corresponding
// row and column number row
function findSum($n)
{
$n--;
$sum = 0;
$sum += ($n * ($n + 1)) / 2;
$sum += ($n * ($n + 1) *
(2 * $n + 1)) / 6;
return $sum;
}
// Driver Code
$n = 3;
echo findSum($n) ;
// This code is contributed
// by nitin mittal.
?>
JavaScript
<script>
// Java script program to find sum of matrix in which
// each element is absolute difference of its
// corresponding row and column number row.
// Return the sum of matrix in which each element
// is absolute difference of its corresponding
// row and column number row
function findSum( n)
{
n--;
let sum = 0;
sum += (n * (n + 1)) / 2;
sum += (n * (n + 1) * (2 * n + 1)) / 6;
return sum;
}
// Driver Code
let n = 3;
document.write(findSum(n));
// This code is contributed by mohan pavan
</script>
Time Complexity: O(1), as we are not using any loops.
Auxiliary Space: O(1), as we are not using any extra space.
Similar Reads
Sum of Matrix where each element is sum of row and column number
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 = 3Output: 36Explanation: A[]: {{2, 3, 4}, {3, 4, 5}, {4, 5, 6}}.
14 min read
Minimum sum of all absolute differences of same column elements in adjacent rows in a given Matrix
Given a matrix mat[][] having N rows and M columns, the task is to find the minimum distance between two adjacent rows where the distance between two rows is defined as the sum of all absolute differences between two elements present at the same column in the two rows. Examples: Input: mat[][] = {{1
5 min read
Sum of matrix element where each elements is integer division of row and column
Consider a N X N matrix where each element is divided by a column number (integer division), i.e. mat[i][j] = floor((i+1)/(j+1)) where 0 <= i < n and 0 <= j < n. The task is to find the sum of all matrix elements. Examples : Input : N = 2 Output : 4 2 X 2 matrix with given constraint: 1
9 min read
Maximum difference of sum of elements in two rows in a matrix
Given a matrix of m*n order, the task is to find the maximum difference between two rows Rj and Ri such that i < j, i.e., we need to find maximum value of sum(Rj) - sum(Ri) such that row i is above row j. Examples: Input : mat[5][4] = {{-1, 2, 3, 4}, {5, 3, -2, 1}, {6, 7, 2, -3}, {2, 9, 1, 4}, {2
10 min read
Minimum difference between adjacent elements of array which contain elements from each row of a matrix
Given a matrix of N rows and M columns, the task is to find the minimum absolute difference between any of the two adjacent elements of an array of size N, which is created by picking one element from each row of the matrix. Note the element picked from row 1 will become arr[0], element picked from
10 min read
Sum of absolute differences of indices of occurrences of each array element | Set 2
Given an array, arr[] consisting of N integers, the task for each array element arr[i] is to print the sum of |i â j| for all possible indices j such that arr[i] = arr[j]. Examples: Input: arr[] = {1, 3, 1, 1, 2}Output: 5 0 3 4 0Explanation:For arr[0], sum = |0 â 0| + |0 â 2| + |0 â 3| = 5.For arr[1
11 min read
Array element with minimum sum of absolute differences | Set 2
Given an array arr[] consisting of N positive integers, the task is to find an array element X such that sum of its absolute differences with every array element is minimum. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 3Explanation: For element arr[0](= 1): |(1 - 1)| + |(2 - 1)| + |(3 - 1)| + |(4
7 min read
Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
Given two integers N and K, the task is to find the count of N-digit numbers such that the absolute difference of adjacent digits in the number is not greater than K.Examples: Input: N = 2, K = 1 Output: 26 Explanation: The numbers are 10, 11, 12, 21, 22, 23, 32, 33, 34, 43, 44, 45, 54, 55, 56, 65,
11 min read
Largest subset where absolute difference of any two element is a power of 2
Given an array arr[] of distinct elements -109 ? ai ? 109. The task is to find the largest sub-set from the given array such that the absolute difference between any two numbers in the sub-set is a positive power of two. If it is not possible to make such sub-set then print -1. Examples: Input: arr[
10 min read
Count of elements whose absolute difference with the sum of all the other elements is greater than k
Given an array arr[] of N integers and an integer K, the task is to find the number of anomalies in the array. An anomaly is a number for which the absolute difference between it and all the other numbers in the array is greater than K. Find the number of anomalies. Examples: Input: arr[] = {1, 3, 5
5 min read