Print matrix after applying increment operations in M ranges
Last Updated :
13 Sep, 2022
Given a 2-D matrix mat[][] of size N * N, initially all the elements of the matrix are 0. A number of queries(M ranges) need to be performed on the matrix where each query consists of four integers X1, Y1, X2 and Y2, the task is to add 1 to all the cells between mat[X1][Y1] and mat[X2][Y2] (including both) and print the contents of the updated matrix in the end.
Examples:
Input: N = 2, q[][] = { { 0, 0, 1, 1 }, { 0, 0, 0, 1 } }
Output:
2 2
1 1
After 1st query: mat[][] = { {1, 1}, {1, 1} }
After 2nd query: mat[][] = { {2, 2}, {1, 1} }
Input: N = 5, q[][] = { { 0, 0, 1, 2 }, { 1, 2, 3, 4 }, { 1, 4, 3, 4 } }
Output:
1 1 1 1 1
1 1 2 1 2
2 2 2 2 2
2 2 2 2 2
0 0 0 0 0
Approach: For each query (X1, Y1) represents the top left cell of the sub-matrix and (X2, Y2) represents the bottom right cell of the sub-matrix. For each top left cell add 1 to the top left element and subtract 1 from the element next to bottom right cell (if any).
Then maintain a running sum of all the elements from the original (now modified) matrix and at every addition, the current sum is the element (updated) at the current position.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void updateMatrix( int n, int q[3][4])
{
int i, j;
int mat[n][n];
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
mat[i][j] = 0;
for (i = 0; i < 3; i++)
{
int X1 = q[i][0];
int Y1 = q[i][1];
int X2 = q[i][2];
int Y2 = q[i][3];
mat[X1][Y1]++;
if (Y2 + 1 < n)
mat[X2][Y2 + 1]--;
else if (X2 + 1 < n)
mat[X2 + 1][0]--;
}
int sum = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
sum += mat[i][j];
cout << sum << " " ;
}
cout << endl;
}
}
int main()
{
int n = 5;
int q[3][4] = {{ 0, 0, 1, 2 },
{ 1, 2, 3, 4 },
{ 1, 4, 3, 4 }};
updateMatrix(n, q);
return 0;
}
|
Java
public class GFG {
static void updateMatrix( int n, int q[][], int mat[][])
{
int i, j;
for (i = 0 ; i < q.length; i++) {
int X1 = q[i][ 0 ];
int Y1 = q[i][ 1 ];
int X2 = q[i][ 2 ];
int Y2 = q[i][ 3 ];
mat[X1][Y1]++;
if (Y2 + 1 < n)
mat[X2][Y2 + 1 ]--;
else if (X2 + 1 < n)
mat[X2 + 1 ][ 0 ]--;
}
int sum = 0 ;
for (i = 0 ; i < n; i++) {
for (j = 0 ; j < n; j++) {
sum += mat[i][j];
System.out.print(sum + " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
int n = 5 ;
int mat[][] = new int [n][n];
int q[][] = { { 0 , 0 , 1 , 2 },
{ 1 , 2 , 3 , 4 },
{ 1 , 4 , 3 , 4 } };
updateMatrix(n, q, mat);
}
}
|
Python3
def updateMatrix(n, q, mat):
for i in range ( 0 , len (q)):
X1 = q[i][ 0 ];
Y1 = q[i][ 1 ];
X2 = q[i][ 2 ];
Y2 = q[i][ 3 ];
mat[X1][Y1] = mat[X1][Y1] + 1 ;
if (Y2 + 1 < n):
mat[X2][Y2 + 1 ] = mat[X2][Y2 + 1 ] - 1 ;
elif (X2 + 1 < n):
mat[X2 + 1 ][ 0 ] = mat[X2 + 1 ][ 0 ] - 1 ;
sum = 0 ;
for i in range ( 0 , n):
for j in range ( 0 , n):
sum = sum + mat[i][j];
print ( sum , end = ' ' );
print ( " " );
n = 5 ;
mat = [[ 0 for i in range (n)]
for i in range (n)];
q = [[ 0 , 0 , 1 , 2 ],
[ 1 , 2 , 3 , 4 ],
[ 1 , 4 , 3 , 4 ]];
updateMatrix(n, q, mat);
|
C#
using System;
public class GFG {
static void updateMatrix( int n, int [,]q, int [,]mat)
{
int i, j;
for (i = 0; i < q.GetLength(0); i++) {
int X1 = q[i,0];
int Y1 = q[i,1];
int X2 = q[i,2];
int Y2 = q[i,3];
mat[X1,Y1]++;
if (Y2 + 1 < n)
mat[X2,Y2 + 1]--;
else if (X2 + 1 < n)
mat[X2 + 1,0]--;
}
int sum = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
sum += mat[i,j];
Console.Write(sum + " " );
}
Console.WriteLine();
}
}
public static void Main()
{
int n = 5;
int [,]mat = new int [n,n];
int [,]q = { { 0, 0, 1, 2 },
{ 1, 2, 3, 4 },
{ 1, 4, 3, 4 } };
updateMatrix(n, q, mat);
}
}
|
PHP
<?php
function updateMatrix( $n , $q , $mat )
{
for ( $i = 0; $i < sizeof( $q ); $i ++)
{
$X1 = $q [ $i ][0];
$Y1 = $q [ $i ][1];
$X2 = $q [ $i ][2];
$Y2 = $q [ $i ][3];
$mat [ $X1 ][ $Y1 ]++;
if ( $Y2 + 1 < $n )
$mat [ $X2 ][ $Y2 + 1]--;
else if ( $X2 + 1 < $n )
$mat [ $X2 + 1][0]--;
}
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
$sum += $mat [ $i ][ $j ];
echo ( $sum . " " );
}
echo ( "\n" );
}
}
$n = 5;
$mat = array_fill (0, $n ,
array_fill (0, $n , 0));
$q = array ( array ( 0, 0, 1, 2 ),
array ( 1, 2, 3, 4 ),
array ( 1, 4, 3, 4 ));
updateMatrix( $n , $q , $mat );
?>
|
Javascript
<script>
function updateMatrix(n, q, mat)
{
let i, j;
for (i = 0; i < q.length; i++) {
let X1 = q[i][0];
let Y1 = q[i][1];
let X2 = q[i][2];
let Y2 = q[i][3];
mat[X1][Y1]++;
if (Y2 + 1 < n)
mat[X2][Y2 + 1]--;
else if (X2 + 1 < n)
mat[X2 + 1][0]--;
}
let sum = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
sum += mat[i][j];
document.write(sum + " " );
}
document.write( "</br>" );
}
}
let n = 5;
let mat = new Array(n);
for (let i = 0; i < n; i++)
{
mat[i] = new Array(n);
for (let j = 0; j < n; j++)
{
mat[i][j] = 0;
}
}
let q = [ [ 0, 0, 1, 2 ],
[ 1, 2, 3, 4 ],
[ 1, 4, 3, 4 ] ];
updateMatrix(n, q, mat);
</script>
|
Output
1 1 1 1 1
1 1 2 1 2
2 2 2 2 2
2 2 2 2 2
0 0 0 0 0
Complexity Analysis:
- Time Complexity: O(n2)
- Auxiliary Space: O(n2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...