Find the Largest Area of Square Inside Two Rectangles - Problem

There exist n rectangles in a 2D plane with edges parallel to the x and y axis. You are given two 2D integer arrays bottomLeft and topRight where:

bottomLeft[i] = [a_i, b_i] represents the bottom-left coordinates of the i-th rectangle

topRight[i] = [c_i, d_i] represents the top-right coordinates of the i-th rectangle

You need to find the maximum area of a square that can fit inside the intersecting region of at least two rectangles. Return 0 if such a square does not exist.

Input & Output

Example 1 — Basic Intersection
$ Input: bottomLeft = [[1,1],[2,2],[3,1]], topRight = [[3,3],[4,4],[4,4]]
Output: 1
💡 Note: Rectangle 0: (1,1) to (3,3), Rectangle 1: (2,2) to (4,4). Their intersection is (2,2) to (3,3) with width=1, height=1. Largest square has side=1, area=1.
Example 2 — Larger Intersection
$ Input: bottomLeft = [[1,1],[2,2],[1,2]], topRight = [[4,4],[4,4],[3,4]]
Output: 4
💡 Note: Rectangle 0: (1,1) to (4,4), Rectangle 1: (2,2) to (4,4). Intersection is (2,2) to (4,4) with width=2, height=2. Largest square has side=2, area=4.
Example 3 — No Intersection
$ Input: bottomLeft = [[1,1],[3,3]], topRight = [[2,2],[4,4]]
Output: 0
💡 Note: Rectangle 0: (1,1) to (2,2), Rectangle 1: (3,3) to (4,4). These rectangles don't intersect, so no square can fit in a shared region.

Constraints

  • 2 ≤ n ≤ 5 × 104
  • bottomLeft[i].length == topRight[i].length == 2
  • -109 ≤ bottomLeft[i][j], topRight[i][j] ≤ 109
  • bottomLeft[i][0] < topRight[i][0] and bottomLeft[i][1] < topRight[i][1]

Visualization

Tap to expand
Find Largest Square in Rectangle Intersections INPUT R1 R2 R3 1 2 3 4 1 2 3 4 bottomLeft: [[1,1],[2,2],[3,1]] topRight: [[3,3],[4,4],[4,4]] R1 R2 R3 Intersect n = 3 rectangles ALGORITHM STEPS 1 Check All Pairs Loop i from 0 to n-1, j from i+1 to n-1 2 Find Intersection left = max(a_i, a_j) right = min(c_i, c_j) bottom = max(b_i, b_j) top = min(d_i, d_j) 3 Calc Square Side width = right - left height = top - bottom side = min(width, height) 4 Track Maximum If side > 0: maxArea = max(maxArea, side * side) Example: R1 and R2 left=max(1,2)=2, right=min(3,4)=3 bottom=max(1,2)=2, top=min(3,4)=3 side=min(3-2,3-2)=1 --> area=1 FINAL RESULT 1 Max square fits in intersection region OUTPUT 1 [OK] Max Area = 1 Square side = 1 1 x 1 = 1 Key Insight: The optimized single-pass approach checks all O(n^2) pairs of rectangles. For each pair, it calculates the intersection region using max/min operations. The largest square that fits is limited by the smaller dimension of the intersection. Track the maximum area found across all valid intersections. TutorialsPoint - Find the Largest Area of Square Inside Two Rectangles | Optimized Single Pass
Asked in
Google 45 Amazon 32 Microsoft 28
28.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen