Print cells with same rectangular sums in a matrix
Given a matrix of m x n matrix, we need to print all those cells at which sum of sub-matrix ended at this cell and sub-matrix starting at this cell is equal to remaining elements. For better understanding please see below diagram,
Examples :
Input : mat[][] = {1, 2, 3, 5,
4, 1, 0, 2,
0, 1, 2, 0,
7, 1, 1, 0};
Output : (1, 1), (2, 2)
In above matrix, cell (1, 1) and cell (2, 2)
are our required cells because,
For cell (1, 1), sum of red and green areas is same
1+2+4+1+0+2+1+2+0+1+1+0 = 3+5+0+7
Same is true for cell (2, 2)
1+2+3+4+1+0+0+1+2+0+1+0 = 5+2+7+1
We need to print all blue boundary cells for
which sum of red area is equal to green area.
First we construct auxiliary sub-matrices similar to the linked article.
We construct two matrices sum[][] and sumr[][] such that sum[i][j] denotes sum of sub-matrix from mat[0][0] to mat[i][j]. And sumr for storing sum till last indices i.e. sumr[i][j] denotes sum of sub-matrix, mat[i][j] to mat[m – 1][n – 1].
Now we can use above matrices for solving this problem, red area shown in above diagram can be calculated by adding corresponding cells from sum and sumr matrices, as mat[i][j] is considered twice while calculating this sum we will subtract it once to get sum of red area. Getting sum of remaining element, the green part, is pretty easy, we will just subtract sum of red part from total sum of given matrix.
So to check whether a particular cell satisfies the given condition we will calculate the sum of red part as explained above and compare it with total sum of matrix, if this sum is half of total sum, current cell satisfies the condition and hence a candidate for result.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
void printCellWithSameRectangularArea( int mat[R][C],
int m, int n)
{
int sum[m][n], sumr[m][n];
int totalSum = 0;
for ( int i = 0; i < m; i++)
{
for ( int j = 0; j < n; j++)
{
sumr[i][j] = sum[i][j] = mat[i][j];
totalSum += mat[i][j];
}
}
for ( int i = 1; i < m; i++)
{
sum[i][0] += sum[i-1][0];
sumr[m-i-1][n-1] += sumr[m-i][n-1];
}
for ( int j = 1; j < n; j++)
{
sum[0][j] += sum[0][j-1];
sumr[m-1][n-j-1] += sumr[m-1][n-j];
}
for ( int i = 1; i < m; i++)
{
for ( int j = 1; j < n; j++)
{
sum[i][j] += sum[i-1][j] + sum[i][j-1] -
sum[i-1][j-1];
sumr[m-i-1][n-j-1] += sumr[m-i][n-j-1] +
sumr[m-i-1][n-j] -
sumr[m-i][n-j];
}
}
for ( int i = 0; i < m; i++)
{
for ( int j = 0; j < n; j++)
{
int mainDiagRectangleSum = sum[i][j] + sumr[i][j] -
mat[i][j];
if (totalSum == 2 * mainDiagRectangleSum)
cout << "(" << i << ", " << j << ")" << endl;
}
}
}
int main()
{
int mat[R][C] =
{
1, 2, 3, 5,
4, 1, 0, 2,
0, 1, 2, 0,
7, 1, 1, 0
};
printCellWithSameRectangularArea(mat, R, C);
return 0;
}
|
Java
class GFG {
static final int R = 4 ;
static final int C = 4 ;
static void printCellWithSameRectangularArea( int mat[][],
int m, int n)
{
int sum[][] = new int [m][n];
int sumr[][] = new int [m][n];
int totalSum = 0 ;
for ( int i = 0 ; i < m; i++) {
for ( int j = 0 ; j < n; j++) {
sumr[i][j] = sum[i][j] = mat[i][j];
totalSum += mat[i][j];
}
}
for ( int i = 1 ; i < m; i++) {
sum[i][ 0 ] += sum[i - 1 ][ 0 ];
sumr[m - i - 1 ][n - 1 ] += sumr[m - i][n - 1 ];
}
for ( int j = 1 ; j < n; j++) {
sum[ 0 ][j] += sum[ 0 ][j - 1 ];
sumr[m - 1 ][n - j - 1 ] += sumr[m - 1 ][n - j];
}
for ( int i = 1 ; i < m; i++) {
for ( int j = 1 ; j < n; j++) {
sum[i][j] += sum[i - 1 ][j] + sum[i][j - 1 ] -
sum[i - 1 ][j - 1 ];
sumr[m - i - 1 ][n - j - 1 ] += sumr[m - i][n - j - 1 ] +
sumr[m - i - 1 ][n - j] -
sumr[m - i][n - j];
}
}
for ( int i = 0 ; i < m; i++) {
for ( int j = 0 ; j < n; j++) {
int mainDiagRectangleSum = sum[i][j] +
sumr[i][j] - mat[i][j];
if (totalSum == 2 * mainDiagRectangleSum)
System.out.println( "(" + i + ", " + j + ")" );
}
}
}
public static void main(String[] args)
{
int mat[][] = {{ 1 , 2 , 3 , 5 },
{ 4 , 1 , 0 , 2 },
{ 0 , 1 , 2 , 0 },
{ 7 , 1 , 1 , 0 }};
printCellWithSameRectangularArea(mat, R, C);
}
}
|
Python3
def printCellWithSameRectangularArea(mat, m, n):
sum = [[ 0 for i in range (m)] for j in range (n)]
sumr = [[ 0 for i in range (m)] for j in range (n)]
totalSum = 0
for i in range (m):
for j in range (n):
sumr[i][j] = sum [i][j] = mat[i][j];
totalSum + = mat[i][j]
for i in range ( 1 ,m):
sum [i][ 0 ] + = sum [i - 1 ][ 0 ]
sumr[m - i - 1 ][n - 1 ] + = sumr[m - i][n - 1 ]
for j in range ( 1 ,n):
sum [ 0 ][j] + = sum [ 0 ][j - 1 ];
sumr[m - 1 ][n - j - 1 ] + = sumr[m - 1 ][n - j]
for i in range ( 1 ,m):
for j in range ( 1 ,n):
sum [i][j] + = sum [i - 1 ][j] + sum [i][j - 1 ] - sum [i - 1 ][j - 1 ]
sumr[m - i - 1 ][n - j - 1 ] + = sumr[m - i][n - j - 1 ] + sumr[m - i - 1 ][n - j] - sumr[m - i][n - j]
for i in range (m):
for j in range (n):
mainDiagRectangleSum = sum [i][j] + sumr[i][j] - mat[i][j]
if (totalSum = = 2 * mainDiagRectangleSum):
print ( "(" ,i, "," ,j, ")" )
mat = [[ 1 , 2 , 3 , 5 ,],
[ 4 , 1 , 0 , 2 ,],
[ 0 , 1 , 2 , 0 ],
[ 7 , 1 , 1 , 0 ]]
printCellWithSameRectangularArea(mat, 4 , 4 )
|
C#
using System;
class GFG {
static int R = 4;
static int C = 4;
static void printCellWithSameRectangularArea( int [,]mat,
int m, int n)
{
int [,]sum = new int [m, n];
int [,]sumr = new int [m, n];
int totalSum = 0;
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < n; j++) {
sumr[i, j] = sum[i, j] = mat[i, j];
totalSum += mat[i, j];
}
}
for ( int i = 1; i < m; i++)
{
sum[i, 0] += sum[i - 1, 0];
sumr[m - i - 1, n - 1] += sumr[m - i, n - 1];
}
for ( int j = 1; j < n; j++)
{
sum[0,j] += sum[0,j - 1];
sumr[m - 1,n - j - 1] += sumr[m - 1,n - j];
}
for ( int i = 1; i < m; i++) {
for ( int j = 1; j < n; j++) {
sum[i,j] += sum[i - 1,j] + sum[i,j - 1] -
sum[i - 1,j - 1];
sumr[m - i - 1,n - j - 1] += sumr[m - i,n - j - 1] +
sumr[m - i - 1,n - j] -
sumr[m - i,n - j];
}
}
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < n; j++) {
int mainDiagRectangleSum = sum[i,j] +
sumr[i,j] - mat[i,j];
if (totalSum == 2 * mainDiagRectangleSum)
Console.WriteLine( "(" + i + ", " + j + ")" );
}
}
}
public static void Main()
{
int [,]mat = {{1, 2, 3, 5},
{4, 1, 0, 2},
{0, 1, 2, 0},
{7, 1, 1, 0}};
printCellWithSameRectangularArea(mat, R, C);
}
}
|
Javascript
<script>
let R = 4;
let C = 4;
function printCellWithSameRectangularArea(mat, m, n)
{
let sum = new Array(m);
let sumr = new Array(m);
let totalSum = 0;
for (let i = 0; i < m; i++)
{
sum[i] = new Array(n);
sumr[i] = new Array(n);
for (let j = 0; j < n; j++)
{
sumr[i][j] = sum[i][j] = mat[i][j];
totalSum += mat[i][j];
}
}
for (let i = 1; i < m; i++)
{
sum[i][0] += sum[i - 1][0];
sumr[m - i - 1][n - 1] += sumr[m - i][n - 1];
}
for (let j = 1; j < n; j++) {
sum[0][j] += sum[0][j - 1];
sumr[m - 1][n - j - 1] += sumr[m - 1][n - j];
}
for (let i = 1; i < m; i++)
{
for (let j = 1; j < n; j++)
{
sum[i][j] += sum[i - 1][j] + sum[i][j - 1] -
sum[i - 1][j - 1];
sumr[m - i - 1][n - j - 1] += sumr[m - i][n - j - 1] +
sumr[m - i - 1][n - j] -
sumr[m - i][n - j];
}
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++)
{
let mainDiagRectangleSum = sum[i][j] +
sumr[i][j] - mat[i][j];
if (totalSum == 2 * mainDiagRectangleSum)
document.write( "(" + i + ", " + j + ")" + "</br>" );
}
}
}
let mat = [[1, 2, 3, 5],
[4, 1, 0, 2],
[0, 1, 2, 0],
[7, 1, 1, 0]];
printCellWithSameRectangularArea(mat, R, C);
</script>
|
Time complexity: O(n x m).
Auxiliary Space: O(n x m).
Last Updated :
06 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...