Maximum Area of a Rotated Rectangle
Math & Bit ManipulationAuto-graded · 21 test casesLast updated
Find the largest surface area of any rectangle formed by four vertices chosen from a given set of 2D points.
You are given a list of unique 2D coordinates representing points on a grid plane.
Select four distinct points from the list that form the four corners of a rectangle. Find and return the maximum area among all valid rectangles that can be created this way.
Note that the sides of the rectangle do not need to be parallel to the coordinate axes (they can be rotated). Squares count as valid rectangles. If no four points form a rectangle, return 0.
Constraints
1 <= len(points) <= 2000 <= points[i][0], points[i][1] <= 40000- All points in the input list are distinct.
- The return value is guaranteed to be an integer.
Examples
Example 1
Input: max_rectangle_area([[1, 2], [2, 1], [1, 0], [0, 1]])
Output: 2
Example 2
Input: max_rectangle_area([[0, 1], [1, 0], [3, 2], [2, 3], [1, 1], [2, 1], [1, 2], [2, 2]])
Output: 4
Example 3
Input: max_rectangle_area([[0, 0], [1, 0], [2, 1], [0, 1]])
Output: 0
Hints
Hint 1
Instead of picking four corners or testing right angles, consider the geometric properties of a rectangle's diagonals.
Hint 2
A quadrilateral is a rectangle if and only if its two diagonals share the same midpoint and have the exact same length.
Hint 3
To keep everything in integer arithmetic, store double the midpoint `(x1 + x2, y1 + y2)` and the squared diagonal length `(x1 - x2)^2 + (y1 - y2)^2` as a hash key.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Instead of checking combinations of four points ($O(N^4)$) or three points ($O(N^3)$), we can leverage a fundamental geometric property of rectangles:
A quadrilateral is a rectangle if and only if its two diagonals share the exact same midpoint and are equal in length.
Algorithm
-
Group Diagonals by Midpoint and Length:
- Iterate over every pair of points $(p_i, p_j)$ where $i < j$.
- Compute the midpoint and diagonal length. To avoid floating-point inaccuracies, store:
2 * midpoint$\rightarrow (x_i + x_j, y_i + y_j)$squared length$\rightarrow (x_i - x_j)^2 + (y_i - y_j)^2$
- Store these pairs in a hash map keyed by
(center_x_2x, center_y_2x, squared_length). We only need to store the index $i$ because $j$ can be dynamically reconstructed from the center coordinates ($x_j = \text{center_x_2x} - x_i$).
-
Compute Rectangular Areas:
- For every key in the hash map that has at least 2 diagonals, any two diagonals in that list form a valid rectangle.
- For a pair of diagonals starting at point $P_1$ and point $P_2$:
- The point opposite $P_2$ across the midpoint is $P_3 = (2 \cdot m_x - P_{2x}, 2 \cdot m_y - P_{2y})$.
- The vectors along the adjacent sides of the rectangle originating from $P_1$ are $\vec{u} = P_2 - P_1$ and $\vec{v} = P_3 - P_1$.
- The surface area of the rectangle is given by the magnitude of the 2D cross product: $|\vec{u}_x \cdot \vec{v}_y - \vec{u}_y \cdot \vec{v}_x|$.
- Track and return the maximum area found across all pairs.
Complexity
- Time Complexity:
- Constructing the hash map takes $O(N^2)$ time for $N$ points.
- Pairwise comparison within groups takes $O(k^2)$ per group of size $k$. Since all diagonals in a group must share a center, $k \le N / 2$. In practice, $k$ is extremely small for typical point distributions, leading to an overall time complexity of $O(N^2)$ on average and $O(N^3)$ worst-case. With $N \le 200$, this executes well within milliseconds.
- Space Complexity: $O(N^2)$ to store up to $\frac{N(N-1)}{2}$ diagonals in the hash map.
Python 3 Reference Solution
from collections import defaultdict
from typing import List
def max_rectangle_area(points: List[List[int]]) -> int:
# Key: (2 * center_x, 2 * center_y, squared_length) -> list of point indices
diagonals = defaultdict(list)
n = len(points)
for i in range(n):
x1, y1 = points[i]
for j in range(i + 1, n):
x2, y2 = points[j]
center_x2 = x1 + x2
center_y2 = y1 + y2
sq_dist = (x1 - x2) ** 2 + (y1 - y2) ** 2
diagonals[(center_x2, center_y2, sq_dist)].append(i)
max_area = 0
for (cx2, cy2, _), start_indices in diagonals.items():
count = len(start_indices)
if count < 2:
continue
# Combine pairs of diagonals that share the same center and length
for a in range(count):
p1_x, p1_y = points[start_indices[a]]
for b in range(a + 1, count):
p2_x, p2_y = points[start_indices[b]]
# Given p2 is one corner of diagonal 2, the opposite corner is p3
p3_x, p3_y = cx2 - p2_x, cy2 - p2_y
# Vectors from p1 to adjacent corners p2 and p3
v1_x, v1_y = p2_x - p1_x, p2_y - p1_y
v2_x, v2_y = p3_x - p1_x, p3_y - p1_y
# Absolute 2D cross product gives the exact rectangle area
area = abs(v1_x * v2_y - v1_y * v2_x)
if area > max_area:
max_area = area
return max_area