Maximum Area Rectangle With Point Constraints I - Problem
You're an architect designing a rectangular foundation on a plot of land with existing structures. Given an array points where points[i] = [xi, yi] represents the coordinates of structures on an infinite plane, your task is to find the maximum area rectangle that can be built such that:

๐Ÿ”น The rectangle uses exactly four of these points as its corners
๐Ÿ”น The rectangle contains no other points inside or on its borders
๐Ÿ”น The rectangle's edges are parallel to the coordinate axes

Return the maximum area achievable, or -1 if no valid rectangle exists. This problem tests your ability to work with computational geometry, coordinate validation, and optimization.

Input & Output

example_1.py โ€” Basic Rectangle
$ Input: points = [[1,1], [1,3], [3,1], [3,3]]
โ€บ Output: 4
๐Ÿ’ก Note: The four points form a perfect 2ร—2 rectangle with no other points inside. The corners are at (1,1), (1,3), (3,1), and (3,3), giving us an area of (3-1) ร— (3-1) = 4.
example_2.py โ€” Point Inside Rectangle
$ Input: points = [[1,1], [1,3], [3,1], [3,3], [2,2]]
โ€บ Output: -1
๐Ÿ’ก Note: Although points (1,1), (1,3), (3,1), (3,3) could form a rectangle, there's a point at (2,2) inside it. Since rectangles cannot contain other points, no valid rectangle exists.
example_3.py โ€” Multiple Valid Rectangles
$ Input: points = [[1,1], [1,3], [3,1], [3,3], [1,5], [3,5]]
โ€บ Output: 4
๐Ÿ’ก Note: Two valid rectangles exist: (1,1)โ†’(3,3) with area 4, and (1,3)โ†’(3,5) with area 4. We return the maximum area, which is 4.

Visualization

Tap to expand
Rectangle Formation: From Diagonals to Valid RectanglesStep 1: Identify Diagonal(x1,y1)(x2,y2)Step 2: Calculate Corners(x1,y2)(x2,y1)Step 3: Hash LookupHash Set: {(1,1), (1,3), (3,1), (3,3), (2,4)}Lookup (1,3): โœ“ FoundLookup (3,1): โœ“ FoundAll corners exist!Time: O(1) per lookupStep 4: Validate BoundaryโŒ Point inside!Rectangle invalid๐Ÿ’ก Key Insights:โ€ข Rectangle uniquely defined by diagonal โ€ข Hash lookup enables O(1) corner verification โ€ข Boundary check prevents invalid rectangles
Understanding the Visualization
1
Diagonal Recognition
Two points form a valid diagonal if they have different x AND y coordinates
2
Corner Calculation
For diagonal (x1,y1)โ†’(x2,y2), other corners are (x1,y2) and (x2,y1)
3
Existence Verification
Use hash table to quickly check if calculated corners exist in point set
4
Boundary Validation
Ensure no other points lie inside or on the rectangle's boundary
Key Takeaway
๐ŸŽฏ Key Insight: By recognizing that any rectangle is uniquely defined by its diagonal, we can reduce the problem from checking O(nโด) combinations to O(nยฒ) diagonal pairs, making the solution much more efficient.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nโด)

Four nested loops to check all combinations of 4 points, where n is the number of points

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables and calculations

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค points.length โ‰ค 50
  • points[i].length == 2
  • 0 โ‰ค xi, yi โ‰ค 5 ร— 104
  • All given points are unique
Asked in
Google 42 Microsoft 35 Amazon 28 Meta 22
38.2K Views
Medium Frequency
~25 min Avg. Time
1.5K 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