Sum of matrix element where each elements is integer division of row and column
Last Updated :
12 Sep, 2023
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 0
2 1
Sum of matrix element: 4
Input : N = 3
Output : 9
Method 1 (Brute Force): Run two loops, one for the row and another for the column, and find the integer part of (i / j) and add to the answer.
Below is the implementation of this approach:
C++
// C++ program to find sum of matrix element
// where each element is integer division of
// row and column.
#include<bits/stdc++.h>
using namespace std;
// Return sum of matrix element where each element
// is division of its corresponding row and column.
int findSum(int n)
{
int ans = 0;
for (int i = 1; i <= n; i++) // for rows
for (int j = 1; j <= n; j++) // for columns
ans += (i/j);
return ans;
}
// Driven Program
int main()
{
int N = 2;
cout << findSum(N) << endl;
return 0;
}
Java
// java program to find sum of matrix
// element where each element is integer
// division of row and column.
import java.io.*;
class GFG {
// Return sum of matrix element
// where each element is division
// of its corresponding row and
// column.
static int findSum(int n)
{
int ans = 0;
// for rows
for (int i = 1; i <= n; i++)
// for columns
for (int j = 1; j <= n; j++)
ans += (i/j);
return ans;
}
// Driven Program
public static void main (String[] args)
{
int N = 2;
System.out.println( findSum(N));
}
}
// This code is contributed by anuj_67.
Python3
# Python 3 program to find sum of
# matrix element where each element
# is integer division of row and column.
# Return sum of matrix element
# where each element is division
# of its corresponding row and column.
def findSum(N):
ans = 0
for i in range(1, N + 1):
for j in range(1, N + 1):
ans += i // j
return ans
# Driver code
N = 2
print(findSum(N))
# This code is contributed
# by Shrikant13
C#
// C# program to find the sum of matrix
// element where each element is an integer
// division of row and column.
using System;
class GFG {
// Return sum of matrix element
// where each element is division
// of its corresponding row and
// column.
static int findSum(int n)
{
int ans = 0;
// for rows
for (int i = 1; i <= n; i++)
// for columns
for (int j = 1; j <= n; j++)
ans += (i/j);
return ans;
}
// Driven Program
public static void Main ()
{
int N = 2;
Console.WriteLine( findSum(N));
}
}
// This code is contributed by anuj_67.
PHP
<?php
// PHP program to find sum of matrix element
// where each element is integer division of
// row and column.
// Return sum of matrix element
// where each element is division
// of its corresponding row and
// column.
function findSum( $n)
{
$ans = 0;
// for rows
for($i = 1; $i <= $n; $i++)
// for columns
for($j = 1; $j <= $n; $j++)
$ans += ($i / $j);
return floor($ans);
}
// Driver Code
$N = 2;
echo findSum($N);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript program to find the sum of matrix
// element where each element is an integer
// division of row and column.
// Return sum of matrix element
// where each element is division
// of its corresponding row and
// column.
function findSum(n)
{
let ans = 0;
// for rows
for (let i = 1; i <= n; i++)
// for columns
for (let j = 1; j <= n; j++)
ans += parseInt(i/j, 10);
return ans;
}
let N = 2;
document.write(findSum(N));
</script>
Time complexity: O(n2).
Auxiliary Space: O(1)
Method 2 (Efficient):
Let N = 9, the matrix will be

Observe, for each jth column
mat[i][k] = 0, for 1 <= k < j, 1 <= i <= N
mat[i][k] = 1, for j <= k < 2*j, 1 <= i <= N
mat[i][k] = 2, for 2*j <= k < 3*j, 1 <= i <= N
and so on.
So, in each column i, there are i - 1 zero,
followed by i times 1, followed by i times 2, and so on.
We traverse matrix column by column and sum elements.
Below is the implementation of this approach.
C++
// C++ program to find sum of matrix element
// where each element is integer division of
// row and column.
#include<bits/stdc++.h>
using namespace std;
// Return sum of matrix element where each
// element is division of its corresponding
// row and column.
int findSum(int n)
{
int ans = 0, temp = 0, num;
// For each column.
for (int i = 1; i <= n && temp < n; i++)
{
// count the number of elements of
// each column. Initialize to i -1
// because number of zeroes are i - 1.
temp = i - 1;
// For multiply
num = 1;
while (temp < n)
{
if (temp + i <= n)
ans += (i * num);
else
ans += ((n - temp) * num);
temp += i;
num ++;
}
}
return ans;
}
// Driven Program
int main()
{
int N = 2;
cout << findSum(N) << endl;
return 0;
}
Java
// java program to find sum of matrix element
// where each element is integer division of
// row and column.
import java.io.*;
class GFG {
// Return sum of matrix element where each
// element is division of its corresponding
// row and column.
static int findSum(int n)
{
int ans = 0, temp = 0, num;
// For each column.
for (int i = 1; i <= n && temp < n; i++)
{
// count the number of elements of
// each column. Initialize to i -1
// because number of zeroes are i - 1.
temp = i - 1;
// For multiply
num = 1;
while (temp < n)
{
if (temp + i <= n)
ans += (i * num);
else
ans += ((n - temp) * num);
temp += i;
num ++;
}
}
return ans;
}
// Driven Program
public static void main (String[] args)
{
int N = 2;
System.out.println(findSum(N));
}
}
// This code is contributed by anuj_67.
Python3
# Program to find sum of matrix element
# where each element is integer division
# of row and column.
# Return sum of matrix element where each
# element is division of its corresponding
# row and column.
def findSum(n):
ans = 0; temp = 0;
for i in range(1, n + 1):
# count the number of elements of
# each column. Initialize to i -1
# because number of zeroes are i - 1.
if temp < n:
temp = i - 1
# For multiply
num = 1
while temp < n:
if temp + i <= n:
ans += i * num
else:
ans += (n - temp) * num
temp += i
num += 1
return ans
# Driver Code
N = 2
print(findSum(N))
# This code is contributed by Shrikant13
C#
// C# program to find sum of matrix
// element where each element is
// integer division of row and column.
using System;
class GFG
{
// Return sum of matrix element
// where each element is division
// of its corresponding row and column.
static int findSum(int n)
{
int ans = 0, temp = 0, num;
// For each column.
for (int i = 1; i <= n && temp < n; i++)
{
// count the number of elements
// of each column. Initialize
// to i -1 because number of
// zeroes are i - 1.
temp = i - 1;
// For multiply
num = 1;
while (temp < n)
{
if (temp + i <= n)
ans += (i * num);
else
ans += ((n - temp) * num);
temp += i;
num ++;
}
}
return ans;
}
// Driver Code
public static void Main ()
{
int N = 2;
Console.WriteLine(findSum(N));
}
}
// This code is contributed by anuj_67.
PHP
<?php
// PHP program to find sum of
// matrix element where each
// element is integer division
// of row and column.
// Return sum of matrix element
// where each element is division
// of its corresponding row and column.
function findSum( $n)
{
$ans = 0; $temp = 0; $num;
// For each column.
for ($i = 1; $i <= $n and
$temp < $n; $i++)
{
// count the number of elements
// of each column. Initialize
// to i -1 because number of
// zeroes are i - 1.
$temp = $i - 1;
// For multiply
$num = 1;
while ($temp < $n)
{
if ($temp + $i <= $n)
$ans += ($i * $num);
else
$ans += (($n - $temp) *
$num);
$temp += $i;
$num ++;
}
}
return $ans;
}
// Driver Code
$N = 2;
echo findSum($N);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// java Script program to find sum of matrix element
// where each element is integer division of
// row and column.
// Return sum of matrix element where each
// element is division of its corresponding
// row and column.
function findSum(n) {
let ans = 0, temp = 0, num;
// For each column.
for (let i = 1; i <= n && temp < n; i++)
{
// count the number of elements of
// each column. Initialize to i -1
// because number of zeroes are i - 1.
temp = i - 1;
// For multiply
num = 1;
while (temp < n)
{
if (temp + i <= n)
ans += (i * num);
else
ans += ((n - temp) * num);
temp += i;
num ++;
}
}
return ans;
}
// Driven Program
let N = 2;
document.write(findSum(N));
// This code is contributed by sravan kumar G
</script>
Time complexity: O(n2)
Auxiliary Space: O(1), since no extra space has been taken.
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
Sum of matrix in which each element is absolute difference of its row and column numbers 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
13 min read
Find sum of all elements in a matrix except the elements in row and/or column of given cell? Given a 2D matrix and a set of cell indexes e.g., an array of (i, j) where i indicates row and j column. For every given cell index (i, j), find sums of all matrix elements except the elements present in i'th row and/or j'th column. Example: mat[][] = { {1, 1, 2} {3, 4, 6} {5, 3, 2} } Array of Cell
12 min read
Generate a Matrix with mean of each subarray of each row as an integer Given two integers M and N, the task is to generate an MxN matrix having elements in range [1, MxN] such that the average of any subarray of any row is an integer. If it is not possible to do so, return -1. Examples: Input: M = 2, N = 3Output: 1 3 5 2 4 6 Explanation: Subarrays of first row with siz
8 min read
Program to find the Sum of each Row and each Column of a Matrix Given a matrix mat of size m à n, the task is to compute the sum of each row and each column of the matrix.Examples:Input: mat = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ] Output: Sum of row 0 = 10 Sum of row 1 = 26 Sum of row 2 = 42 Sum of row 3 = 58 Sum of column 0 = 28 Sum
7 min read
Find a Square Matrix such that sum of elements in every row and column is K Given two integers N and K, the task is to find an N x N square matrix such that sum of every row and column should be equal to K. Note that there can be multiple such matrices possible. Print any one of them.Examples: Input: N = 3, K = 15 Output: 2 7 6 9 5 1 4 3 8Input: N = 3, K = 7 Output: 7 0 0 0
4 min read
Find row and column pair in given Matrix with equal row and column sum Given a matrix Mat of size N x M, the task is to find all the pairs of rows and columns where the sum of elements in the row is equal to the sum of elements in the columns. Examples: Input: M = {{1, 2, 2}, {1, 5, 6}, {3, 8, 9}}Output: {{1, 1}}Explanation: The sum of elements of rows and columns of m
8 min read
Move matrix elements in given direction and add elements with same value Given a matrix m[ ][ ] of size n x n consisting of integers and given a character 'x' indicating the direction. Value of 'x' can be 'u', 'd', 'l', 'r' indicating Up, Down, Left, Right correspondingly. The task is to move the element to given direction such that the consecutive elements having same v
15+ min read
Sum of main diagonal elements in a Matrix which are prime Given a matrix mat[][] of R rows and C columns. The task is to find the sum of all elements from the main diagonal which are prime numbers. Note: The main diagonals are the ones that occur from Top Left of Matrix Down To Bottom Right Corner. Examples: Input: R = 3, C = 3, mat[][] = {{1, 2, 3}, {0, 1
14 min read
Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y Given four integers N, M, X and Y, the task is to construct a N * M matrix such that each cell consists of a value in the range [0, X] such that sum of any two adjacent cells should be less than or equal to Y and the total sum of the matrix should be maximum. Example: Input: N = 3, M = 3, X = 5, Y =
7 min read