Maximum number of overlapping rectangles with at least one common point | C # program for the above approach ; Function to find the maximum number of overlapping rectangles ; Stores the maximum count of overlapping rectangles ; Stores the X and Y coordinates ; Iterate over all pairs of Xs and Ys ; Store the count for the current X and Y ; Update the maximum count of rectangles ; Returns the total count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void maxOverlappingRectangles ( int [ ] x1 , int [ ] y1 , int [ ] x2 , int [ ] y2 , int N ) { int max_rectangles = 0 ; List < int > X = new List < int > ( ) ; List < int > Y = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { X . Add ( x1 [ i ] ) ; X . Add ( x2 [ i ] - 1 ) ; Y . Add ( y1 [ i ] ) ; Y . Add ( y2 [ i ] - 1 ) ; } for ( int i = 0 ; i < X . Count ; i ++ ) { for ( int j = 0 ; j < Y . Count ; j ++ ) { 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 ++ ; } } max_rectangles = Math . Max ( max_rectangles , cnt ) ; } } Console . WriteLine ( max_rectangles ) ; } public static void Main ( ) { int [ ] x1 = { 0 , 50 } ; int [ ] y1 = { 0 , 50 } ; int [ ] x2 = { 100 , 60 } ; int [ ] y2 = { 100 , 60 } ; int N = x1 . Length ; maxOverlappingRectangles ( x1 , y1 , x2 , y2 , N ) ; } }