Minimum Area Rectangle II - Problem

Given an array of points in the X-Y plane, find the minimum area of any rectangle that can be formed from these points.

The key challenge here is that the rectangle's sides don't need to be parallel to the X and Y axes - they can be rotated at any angle! This means we need to consider all possible rectangles, not just axis-aligned ones.

Input: An array points where points[i] = [x_i, y_i] represents a point in 2D space.

Output: The minimum area of any rectangle formed from these points, or 0 if no rectangle can be formed.

Note: Answers within 10^-5 of the actual answer will be accepted due to floating point precision.

Input & Output

example_1.py โ€” Basic Rectangle
$ Input: points = [[1,2],[2,1],[1,0],[0,1]]
โ€บ Output: 2.0
๐Ÿ’ก Note: These four points form a rotated square. The diagonal pairs are (1,2)โ†”(1,0) and (2,1)โ†”(0,1). Both diagonals have length โˆš8 and intersect at center (1,1). The side length is โˆš2, so area = (โˆš2)ยฒ = 2.0
example_2.py โ€” Multiple Rectangles
$ Input: points = [[0,1],[2,1],[1,1],[1,0],[2,0]]
โ€บ Output: 1.0
๐Ÿ’ก Note: Multiple rectangles can be formed from these points. The smallest one uses points [1,1], [2,1], [2,0], [1,0] forming a unit square with area 1.0. Other larger rectangles exist but have greater areas.
example_3.py โ€” No Rectangle Possible
$ Input: points = [[1,1],[1,3],[3,1]]
โ€บ Output: 0.0
๐Ÿ’ก Note: Only 3 points are given, but we need exactly 4 points to form a rectangle. Since no rectangle can be formed, return 0.0.

Visualization

Tap to expand
Rectangle Detection: Diagonal PropertiesCenterCenterRectangle 1: Area = 4800Rectangle 2: Area = 8000Minimum Area = 4800Key Insight: Rectangles have diagonals with equal length that bisect each otherAlgorithm: Group point pairs by diagonal center and length, then find matching pairs
Understanding the Visualization
1
Generate All Pairs
Consider every pair of points as potential rectangle diagonals
2
Calculate Center & Length
For each diagonal, find its midpoint and length
3
Group by Properties
Pairs with same center and length form rectangles
4
Compute Areas
Calculate area using cross product of adjacent sides
Key Takeaway
๐ŸŽฏ Key Insight: Rectangle diagonals are equal in length and bisect each other - this property lets us efficiently group potential rectangle corners and avoid checking all 4-point combinations.

Time & Space Complexity

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

We check all combinations of 4 points, which is C(n,4) = O(n^4)

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

Only using constant extra space for calculations

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค points.length โ‰ค 50
  • -50 โ‰ค xi, yi โ‰ค 50
  • All points are distinct
  • Answers within 10-5 of actual answer will be accepted
Asked in
Google 15 Meta 12 Microsoft 8 Amazon 6
23.5K Views
Medium Frequency
~25 min Avg. Time
847 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