Minimum Area Rectangle - Problem

You are given an array of points in the X-Y plane points where points[i] = [xi, yi].

Return the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes.

If there is no such rectangle, return 0.

Input & Output

Example 1 — Basic Rectangle
$ Input: points = [[1,1],[1,3],[3,1],[3,3]]
Output: 4
💡 Note: The four points form a rectangle with corners at (1,1), (1,3), (3,1), (3,3). Width = 3-1 = 2, Height = 3-1 = 2, Area = 2×2 = 4.
Example 2 — Multiple Rectangles
$ Input: points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
💡 Note: Even with an extra point (2,2), the same rectangle from example 1 is still the only valid rectangle, so minimum area remains 4.
Example 3 — No Rectangle Possible
$ Input: points = [[1,1],[1,3],[3,1]]
Output: 0
💡 Note: Only 3 points provided. Need at least 4 points to form a rectangle, so return 0.

Constraints

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

Visualization

Tap to expand
Minimum Area Rectangle INPUT 1 3 1 3 (1,1) (1,3) (3,1) (3,3) Points Array: [[1,1], [1,3], [3,1], [3,3]] 4 points in X-Y plane Find min area rectangle ALGORITHM STEPS 1 Create Hash Set Store all points in set for O(1) lookup 2 Pick Diagonal Pairs For each point pair, treat as diagonal corners 3 Check Other Corners Verify if (x1,y2) and (x2,y1) exist in set 4 Calculate Min Area Area = |x2-x1| * |y2-y1| Track minimum found Hash Set Contents: (1,1) (1,3) (3,1) (3,3) O(1) lookup time FINAL RESULT (1,3) (3,3) (1,1) (3,1) width = 2 h=2 Area Calculation: |3-1| x |3-1| = 2 x 2 = 4 Output: 4 Minimum area found! Key Insight: Using a Hash Set enables O(1) point lookup. For each pair of points treated as diagonal corners, we check if the other two corners exist. This reduces the problem from O(n^4) brute force to O(n^2). Points with same x or y cannot form diagonal, so we skip those pairs (ensures non-zero area). TutorialsPoint - Minimum Area Rectangle | Hash Approach
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
67.5K Views
Medium Frequency
~25 min Avg. Time
887 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