The Wayback Machine - https://web.archive.org/web/20211005072719/https://www.geeksforgeeks.org/maximum-number-of-overlapping-rectangles-with-at-least-one-common-point/
Skip to content
Related Articles

Related Articles

Improve Article

Maximum number of overlapping rectangles with at least one common point

  • Last Updated : 05 Oct, 2021

Given four arrays x1[], x2[], y1[], y2[] of size N where (x1[i], y1[i]) denotes the left-bottom corner and (x2[i], y2[i]) right-top corner of a rectangle, the task is to find the maximum number of overlapping rectangles with at least one common point.

Examples:

Input: N = 2 x1[] = {0, 50} y1[] = {0, 50} x2[] = {100, 60} y2[] = {100, 60}
Output: 2
Explanation: There are two rectangles {{0, 0}, {100, 100}}, {{50, 50}, {60, 60}} and both of them are intersecting as shown in the given figure below:

Input: N = 2 x1[] = {0, 100} y1[] = {0, 100} x2[] = {100, 200} y2[] = {100, 200}
Output: 1



Approach: The given problem can be solved by using the Greedy Approach which is based on the idea is that every rectangle on the coordinate plane has its own region and when multiple rectangles are added on the same plane they create intersections between each other. Therefore, to select the maximum number of rectangles overlapping on the common area, greedily choose the area of 1×1 unit as all overlapping areas will have at least this much block. Follow the steps below to solve the given problem:

  • Since there are N rectangles and each rectangle have 2 X-coordinates and 2 Y-coordinates. There will be a total of 2*N number of X -coordinates and Y-coordinates.
  • Therefore, create a vector of X and Y coordinates and push all the X’s and Y’s from the rectangles in the respective vectors.
  • Initialize a variable, say maxRectangles as 0 that stores the maximum overlapping rectangles.
  • Traverse the vector X[] and for each coordinate X[i], traverse the vector Y[] and find the number of overlapping rectangles with X[i] and after this step, update the value of maxRectangles to a maximum of maxRectangles and count obtained for the current iteration.
  • After completing the above steps, print the value of maxRectangles as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the maximum number
// of overlapping rectangles
void maxOverlappingRectangles(
    int x1[], int y1[], int x2[],
    int y2[], int N)
{
    // Stores the maximum count of
    // overlapping rectangles
    int max_rectangles = 0;
  
    // Stores the X and Y coordinates
    vector<int> X, Y;
  
    for (int i = 0; i < N; i++) {
        X.push_back(x1[i]);
        X.push_back(x2[i] - 1);
        Y.push_back(y1[i]);
        Y.push_back(y2[i] - 1);
    }
  
    // Iterate over all pairs of Xs and Ys
    for (int i = 0; i < X.size(); i++) {
  
        for (int j = 0; j < Y.size(); j++) {
  
            // Store the count for the
            // current X and Y
            int cnt = 0;
            for (int k = 0; k < N; k++) {
  
                if (X[i] >= x1[k]
                    && X[i] + 1 <= x2[k]
                    && Y[j] >= y1[k]
                    && Y[j] + 1 <= y2[k]) {
                    cnt++;
                }
            }
  
            // Update the maximum count of
            // rectangles
            max_rectangles = max(
                max_rectangles, cnt);
        }
    }
  
    // Returns the total count
    cout << max_rectangles;
}
  
// Driver Code
int main()
{
    int x1[] = { 0, 50 };
    int y1[] = { 0, 50 };
    int x2[] = { 100, 60 };
    int y2[] = { 100, 60 };
    int N = sizeof(x1) / sizeof(x1[0]);
  
    maxOverlappingRectangles(
        x1, y1, x2, y2, N);
  
    return 0;
}
Output:
2


Time Complexity: O(N3)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :