Total number of unit cells covered by all given Rectangles
Last Updated :
05 May, 2021
Given a matrix A[][] consisting of coordinates of N rectangles such that {A[i][0], A[i][1]} representing bottom left coordinate of rectangle and {A[i][2], A[i][3]} representing top right coordinate of rectangle, the task is to find the total number of cells covered by all rectangles.
Examples:
Input: N = 2,
A[][] = {{1, 1, 3, 3}, {2, 1, 4, 2}}
Output: 5
Explanation:
First rectangle covers the following 4 cells:
{(1, 1), (1, 2), (2, 2), (2, 1)}, {(1, 2), (1, 3), (2, 3), (2, 2)}, {(2, 1), (2, 2), (3, 2), (3, 1)}, {(2, 2), (2, 3), (3, 3), (3, 2)}
Second rectangle encloses 2 cells:
{(2, 1), (2, 2), (3, 2), (3, 1)}, {(3, 1), (3, 2), (4, 2), (4, 1)}
Hence, both the rectangles cover 5 cells (since, 1 cell overlaps)
Input: N = 3,
A[][] = {{1, 3, 4, 5}, {3, 1, 7, 4}, {5, 3, 8, 6}}
Output: 24
Approach:
Follow the steps below to solve the problem:
- Create a Boolean matrix arr[MAX][MAX], where arr[i][j] = 1 if(i, j) is enclosed by any of the given rectangles. Otherwise, arr[i][j] = 0.
- For every rectangle, iterate from bottom left to top right and for all (i, j) boxes mark them arr[i][j] = 1.
- Finally, we will maintain a variable area to store the number of cells enclosed. Increment area if arr[i][j] == 1.
- Print the final value of area.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1001
bool arr[MAX][MAX];
void updateArray( int x1, int y1,
int x2, int y2)
{
for ( int i = x1; i < x2; i++) {
for ( int j = y1; j < y2; j++) {
arr[i][j] = true ;
}
}
}
int findAreaCovered()
{
int area = 0;
for ( int i = 0; i < MAX; i++) {
for ( int j = 0; j < MAX; j++) {
if (arr[i][j] == true ) {
area++;
}
}
}
return area;
}
int main()
{
int N = 3;
vector<vector< int > > A = {
{ 1, 3, 4, 5 },
{ 3, 1, 7, 4 },
{ 5, 3, 8, 6 }
};
for ( int i = 0; i < N; i++) {
updateArray(A[i][0], A[i][1],
A[i][2], A[i][3]);
}
int area = findAreaCovered();
cout << area;
return 0;
}
|
Java
class GFG{
static final int MAX = 1001 ;
static boolean [][]arr = new boolean [MAX][MAX];
static void updateArray( int x1, int y1,
int x2, int y2)
{
for ( int i = x1; i < x2; i++)
{
for ( int j = y1; j < y2; j++)
{
arr[i][j] = true ;
}
}
}
static int findAreaCovered()
{
int area = 0 ;
for ( int i = 0 ; i < MAX; i++)
{
for ( int j = 0 ; j < MAX; j++)
{
if (arr[i][j] == true )
{
area++;
}
}
}
return area;
}
public static void main(String[] args)
{
int N = 3 ;
int [][]A = { { 1 , 3 , 4 , 5 },
{ 3 , 1 , 7 , 4 },
{ 5 , 3 , 8 , 6 } };
for ( int i = 0 ; i < N; i++)
{
updateArray(A[i][ 0 ], A[i][ 1 ],
A[i][ 2 ], A[i][ 3 ]);
}
int area = findAreaCovered();
System.out.print(area);
}
}
|
Python3
MAX = 1001
arr = [[ False for i in range ( MAX )]
for j in range ( MAX )]
def updateArray(x1, y1, x2, y2):
for i in range (x1, x2):
for j in range (y1, y2):
arr[i][j] = True
def findAreaCovered():
area = 0
for i in range ( MAX ):
for j in range ( MAX ):
if arr[i][j]:
area + = 1
return area
if __name__ = = "__main__" :
N = 3
A = [ [ 1 , 3 , 4 , 5 ],
[ 3 , 1 , 7 , 4 ],
[ 5 , 3 , 8 , 6 ] ];
for i in range (N):
updateArray(A[i][ 0 ], A[i][ 1 ],
A[i][ 2 ], A[i][ 3 ]);
area = findAreaCovered();
print (area)
|
C#
using System;
class GFG{
static readonly int MAX = 1001;
static bool [,]arr = new bool [MAX, MAX];
static void updateArray( int x1, int y1,
int x2, int y2)
{
for ( int i = x1; i < x2; i++)
{
for ( int j = y1; j < y2; j++)
{
arr[i, j] = true ;
}
}
}
static int findAreaCovered()
{
int area = 0;
for ( int i = 0; i < MAX; i++)
{
for ( int j = 0; j < MAX; j++)
{
if (arr[i, j] == true )
{
area++;
}
}
}
return area;
}
public static void Main(String[] args)
{
int N = 3;
int [,]A = { { 1, 3, 4, 5 },
{ 3, 1, 7, 4 },
{ 5, 3, 8, 6 } };
for ( int i = 0; i < N; i++)
{
updateArray(A[i, 0], A[i, 1],
A[i, 2], A[i, 3]);
}
int area = findAreaCovered();
Console.Write(area);
}
}
|
Javascript
<script>
let MAX = 1001;
let arr = new Array(MAX);
for ( var i = 0; i < arr.length; i++) {
arr[i] = new Array(2);
}
function updateArray(x1, y1, x2, y2)
{
for (let i = x1; i < x2; i++)
{
for (let j = y1; j < y2; j++)
{
arr[i][j] = true ;
}
}
}
function findAreaCovered()
{
let area = 0;
for (let i = 0; i < MAX; i++)
{
for (let j = 0; j < MAX; j++)
{
if (arr[i][j] == true )
{
area++;
}
}
}
return area;
}
let N = 3;
let A = [[ 1, 3, 4, 5 ],
[ 3, 1, 7, 4 ],
[ 5, 3, 8, 6 ]];
for (let i = 0; i < N; i++)
{
updateArray(A[i][0], A[i][1],
A[i][2], A[i][3]);
}
let area = findAreaCovered();
document.write(area);
</script>
|
Time Complexity : O (N 3)
Auxiliary Space : O (N 2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...