Find the Largest Area of Square Inside Two Rectangles - Problem

Imagine you're a city planner working with overlapping development zones! You have n rectangles on a 2D plane, each representing a development area with edges parallel to the x and y axes.

You're given two arrays:

  • bottomLeft[i] = [a_i, b_i] - the bottom-left coordinates of rectangle i
  • topRight[i] = [c_i, d_i] - the top-right coordinates of rectangle i

Your mission: Find the maximum area of a square that can fit inside the intersecting region of at least two rectangles. If no such intersection exists where a square can fit, return 0.

Key Challenge: The square must be entirely contained within the overlapping area of at least two rectangles, and we want the largest possible square area!

Input & Output

example_1.py β€” Basic Intersection
$ Input: bottomLeft = [[1,1],[2,2],[3,1]], topRight = [[3,3],[4,4],[4,4]]
β€Ί Output: 1
πŸ’‘ Note: Rectangle 1: (1,1) to (3,3), Rectangle 2: (2,2) to (4,4), Rectangle 3: (3,1) to (4,4). The intersection of rectangles 1 and 2 is (2,2) to (3,3) with area 1Γ—1=1. The largest square has area 1.
example_2.py β€” No Intersection
$ Input: bottomLeft = [[1,1],[2,2],[1,2]], topRight = [[2,2],[3,3],[3,3]]
β€Ί Output: 1
πŸ’‘ Note: Rectangle 1: (1,1) to (2,2), Rectangle 2: (2,2) to (3,3), Rectangle 3: (1,2) to (3,3). Rectangles 2 and 3 intersect from (2,2) to (3,3), giving a 1Γ—1 square with area 1.
example_3.py β€” Large Intersection
$ Input: bottomLeft = [[1,1],[3,3],[3,1]], topRight = [[5,5],[6,6],[6,4]]
β€Ί Output: 4
πŸ’‘ Note: Rectangle 1: (1,1) to (5,5), Rectangle 2: (3,3) to (6,6), Rectangle 3: (3,1) to (6,4). The intersection of rectangles 1 and 3 is (3,1) to (5,4) with dimensions 2Γ—3. The largest square has side length 2, so area = 4.

Constraints

  • n == bottomLeft.length == topRight.length
  • 2 ≀ n ≀ 103
  • bottomLeft[i].length == topRight[i].length == 2
  • 1 ≀ bottomLeft[i][0] < topRight[i][0] ≀ 107
  • 1 ≀ bottomLeft[i][1] < topRight[i][1] ≀ 107
  • Each rectangle is guaranteed to be valid (bottom-left coordinates are less than top-right)

Visualization

Tap to expand
Rectangle 1Rectangle 2IntersectionStep-by-Step Process:1. Identify overlapping rectangles2. Calculate intersection bounds: left = max(x1_min, x2_min) right = min(x1_max, x2_max) bottom = max(y1_min, y2_min) top = min(y1_max, y2_max)3. Find largest square: width = right - left = 100 height = top - bottom = 100 side = min(width, height) = 100 area = sideΒ² = 10,000
Understanding the Visualization
1
Setup Rectangles
Place rectangles on 2D coordinate plane
2
Find Intersections
Calculate overlapping regions between rectangle pairs
3
Calculate Squares
For each intersection, find the largest possible square
4
Return Maximum
Select the intersection with the largest square area
Key Takeaway
🎯 Key Insight: The largest square in any rectangle has side length equal to min(width, height). Check all rectangle pairs to find the maximum square area in their intersections.
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
12.2K Views
Medium Frequency
~25 min Avg. Time
486 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