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++
#include<bits/stdc++.h>
using namespace std;
int findSum( int n)
{
int ans = 0;
for ( int i = 1; i <= n; i++)
for ( int j = 1; j <= n; j++)
ans += (i/j);
return ans;
}
int main()
{
int N = 2;
cout << findSum(N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findSum( int n)
{
int ans = 0 ;
for ( int i = 1 ; i <= n; i++)
for ( int j = 1 ; j <= n; j++)
ans += (i/j);
return ans;
}
public static void main (String[] args)
{
int N = 2 ;
System.out.println( findSum(N));
}
}
|
Python3
def findSum(N):
ans = 0
for i in range ( 1 , N + 1 ):
for j in range ( 1 , N + 1 ):
ans + = i / / j
return ans
N = 2
print (findSum(N))
|
C#
using System;
class GFG {
static int findSum( int n)
{
int ans = 0;
for ( int i = 1; i <= n; i++)
for ( int j = 1; j <= n; j++)
ans += (i/j);
return ans;
}
public static void Main ()
{
int N = 2;
Console.WriteLine( findSum(N));
}
}
|
PHP
<?php
function findSum( $n )
{
$ans = 0;
for ( $i = 1; $i <= $n ; $i ++)
for ( $j = 1; $j <= $n ; $j ++)
$ans += ( $i / $j );
return floor ( $ans );
}
$N = 2;
echo findSum( $N );
?>
|
Javascript
<script>
function findSum(n)
{
let ans = 0;
for (let i = 1; i <= n; i++)
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++
#include<bits/stdc++.h>
using namespace std;
int findSum( int n)
{
int ans = 0, temp = 0, num;
for ( int i = 1; i <= n && temp < n; i++)
{
temp = i - 1;
num = 1;
while (temp < n)
{
if (temp + i <= n)
ans += (i * num);
else
ans += ((n - temp) * num);
temp += i;
num ++;
}
}
return ans;
}
int main()
{
int N = 2;
cout << findSum(N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findSum( int n)
{
int ans = 0 , temp = 0 , num;
for ( int i = 1 ; i <= n && temp < n; i++)
{
temp = i - 1 ;
num = 1 ;
while (temp < n)
{
if (temp + i <= n)
ans += (i * num);
else
ans += ((n - temp) * num);
temp += i;
num ++;
}
}
return ans;
}
public static void main (String[] args)
{
int N = 2 ;
System.out.println(findSum(N));
}
}
|
Python3
def findSum(n):
ans = 0 ; temp = 0 ;
for i in range ( 1 , n + 1 ):
if temp < n:
temp = i - 1
num = 1
while temp < n:
if temp + i < = n:
ans + = i * num
else :
ans + = (n - temp) * num
temp + = i
num + = 1
return ans
N = 2
print (findSum(N))
|
C#
using System;
class GFG
{
static int findSum( int n)
{
int ans = 0, temp = 0, num;
for ( int i = 1; i <= n && temp < n; i++)
{
temp = i - 1;
num = 1;
while (temp < n)
{
if (temp + i <= n)
ans += (i * num);
else
ans += ((n - temp) * num);
temp += i;
num ++;
}
}
return ans;
}
public static void Main ()
{
int N = 2;
Console.WriteLine(findSum(N));
}
}
|
PHP
<?php
function findSum( $n )
{
$ans = 0; $temp = 0; $num ;
for ( $i = 1; $i <= $n and
$temp < $n ; $i ++)
{
$temp = $i - 1;
$num = 1;
while ( $temp < $n )
{
if ( $temp + $i <= $n )
$ans += ( $i * $num );
else
$ans += (( $n - $temp ) *
$num );
$temp += $i ;
$num ++;
}
}
return $ans ;
}
$N = 2;
echo findSum( $N );
?>
|
Javascript
<script>
function findSum(n) {
let ans = 0, temp = 0, num;
for (let i = 1; i <= n && temp < n; i++)
{
temp = i - 1;
num = 1;
while (temp < n)
{
if (temp + i <= n)
ans += (i * num);
else
ans += ((n - temp) * num);
temp += i;
num ++;
}
}
return ans;
}
let N = 2;
document.write(findSum(N));
</script>
|
Time complexity: O(n2)
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...