Minimum Area Rectangle II - Problem

You are given an array of points in the X-Y plane points where points[i] = [xi, yi]. Return the minimum area of any rectangle formed from these points, with sides not necessarily parallel to the X and Y axes.

If there is no such rectangle, return 0.

Answers within 10-5 of the actual answer will be accepted.

Input & Output

Example 1 — Basic Rectangle
$ Input: points = [[1,2],[2,1],[1,0],[0,1]]
Output: 2.0
💡 Note: These 4 points form a square rotated 45 degrees with side length √2, so area = (√2)² = 2.0
Example 2 — No Rectangle
$ Input: points = [[1,1],[1,3],[3,1]]
Output: 0.0
💡 Note: Only 3 points given, cannot form any rectangle, return 0
Example 3 — Multiple Rectangles
$ Input: points = [[0,0],[1,0],[2,0],[0,1],[1,1],[2,1]]
Output: 1.0
💡 Note: Multiple rectangles possible, smallest has area 1.0 (1×1 square)

Constraints

  • 1 ≤ points.length ≤ 50
  • points[i].length == 2
  • 0 ≤ xi, yi ≤ 4 × 104
  • All the given points are unique

Visualization

Tap to expand
Minimum Area Rectangle II Diagonal-Based Approach INPUT X Y (1,2) (2,1) (1,0) (0,1) Input Array: points = [[1,2],[2,1], [1,0],[0,1]] 4 points forming a diamond (tilted square) ALGORITHM STEPS 1 Find All Diagonals For each pair of points, compute center and length 2 Group by Center + Length Same center and diagonal length = valid rectangle 3 Calculate Areas Area = |d1 x d2| / 2 (cross product of diagonals) 4 Find Minimum Track smallest area among all valid rectangles Diagonals Share Center center FINAL RESULT sqrt(2) sqrt(2) Area Calculation: sqrt(2) * sqrt(2) = 2.0 Output: 2.0 OK - Minimum area rectangle found! Key Insight: A rectangle's diagonals bisect each other and have equal length. By grouping point pairs that share the same midpoint (center) and diagonal length, we identify potential rectangles. For each valid pair of diagonals, calculate area using cross product: Area = |d1 x d2| / 2. Time complexity: O(n^2) TutorialsPoint - Minimum Area Rectangle II | Diagonal-Based Approach
Asked in
Google 15 Amazon 8 Microsoft 12
28.0K Views
Medium Frequency
~25 min Avg. Time
834 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