Maximum number of overlapping rectangles with at least one common point | 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
function maxOverlappingRectangles ( x1 , y1 , x2 , y2 , N ) { let max_rectangles = 0 ; let X = [ ] , Y = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { X . push ( x1 [ i ] ) ; X . push ( x2 [ i ] - 1 ) ; Y . push ( y1 [ i ] ) ; Y . push ( y2 [ i ] - 1 ) ; } for ( let i = 0 ; i < X . length ; i ++ ) { for ( let j = 0 ; j < Y . length ; j ++ ) { let cnt = 0 ; for ( let 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 ) ; } } document . write ( max_rectangles ) ; } let x1 = [ 0 , 50 ] ; let y1 = [ 0 , 50 ] ; let x2 = [ 100 , 60 ] ; let y2 = [ 100 , 60 ] ; let N = x1 . length ; maxOverlappingRectangles ( x1 , y1 , x2 , y2 , N ) ;