Watch 4 video solutions for Maximum Area Rectangle With Point Constraints I, a medium level problem involving Array, Math, Binary Indexed Tree. This walkthrough by Aryan Mittal has 2,123 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an array points where points[i] = [xi, yi] represents the coordinates of a point on an infinite plane.
Your task is to find the maximum area of a rectangle that:
Return the maximum area that you can obtain or -1 if no such rectangle is possible.
Example 1:
Input: points = [[1,1],[1,3],[3,1],[3,3]]
Output: 4
Explanation:

We can make a rectangle with these 4 points as corners and there is no other point that lies inside or on the border. Hence, the maximum possible area would be 4.
Example 2:
Input: points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: -1
Explanation:

There is only one rectangle possible is with points [1,1], [1,3], [3,1] and [3,3] but [2,2] will always lie inside it. Hence, returning -1.
Example 3:
Input: points = [[1,1],[1,3],[3,1],[3,3],[1,2],[3,2]]
Output: 2
Explanation:

The maximum area rectangle is formed by the points [1,3], [1,2], [3,2], [3,3], which has an area of 2. Additionally, the points [1,1], [1,2], [3,1], [3,2] also form a valid rectangle with the same area.
Constraints:
1 <= points.length <= 10points[i].length == 20 <= xi, yi <= 100Problem Overview: You are given a list of 2D points on a plane. The task is to find the maximum area of an axis-aligned rectangle whose four corners exist in the input and that does not contain any additional points inside or on its edges. If no valid rectangle exists, return 0.
Approach 1: Brute Force Corner Enumeration (O(n^4) time, O(1) space)
Check every combination of four points and determine whether they form an axis-aligned rectangle. For a valid rectangle, two points must share the same x-coordinate (left/right edges) and two must share the same y-coordinate (top/bottom edges). After identifying a rectangle candidate, iterate through all points again to ensure no other point lies inside or on the rectangle boundary except the four corners. This approach is straightforward but extremely slow because it evaluates every 4-point combination.
Approach 2: Diagonal Enumeration with Point Set (O(n^3) time, O(n) space)
Instead of choosing four corners directly, treat pairs of points as potential diagonally opposite corners. For two points (x1, y1) and (x2, y2) where x1 != x2 and y1 != y2, the other two rectangle corners must be (x1, y2) and (x2, y1). Store all points in a hash set for O(1) membership checks and verify that these two complementary corners exist. Once all four corners are confirmed, scan the points to ensure no extra point lies within the rectangle or along its edges. Compute the area using abs(x1 - x2) * abs(y1 - y2) and keep the maximum. This enumeration significantly reduces combinations compared to brute force while remaining simple to implement.
Sorting the points by coordinates can help with deterministic iteration and pruning, though the core idea relies on fast lookups in a set. The approach mainly involves coordinate comparisons and membership checks, making it a natural application of geometry reasoning combined with array traversal and optional sorting.
Recommended for interviews: Diagonal enumeration with a hash set. Interviewers expect you to recognize that a rectangle can be uniquely determined by its diagonal corners. The brute force version shows the baseline idea, but the diagonal enumeration demonstrates better algorithmic thinking and reduces the search space from O(n^4) to O(n^3).
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force 4-Point Enumeration | O(n^4) | O(1) | Conceptual baseline or very small input sizes |
| Diagonal Enumeration with Hash Set | O(n^3) | O(n) | General solution used in interviews and competitive programming |