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 <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a scatter of dots on a giant sheet of paper and you want to find the biggest rectangle you can draw using some of these dots as corners. The brute force approach checks every possible combination of four dots to see if they form a valid rectangle, and if so, how big it is.
Here's how the algorithm would work step-by-step:
def max_rectangle_area(point_list): maximum_area_found = -1 total_points = len(point_list) # We iterate through every combination of four distinct points to evaluate potential rectangles for first_index in range(total_points): for second_index in range(first_index + 1, total_points): for third_index in range(second_index + 1, total_points): for fourth_index in range(third_index + 1, total_points): point_one = point_list[first_index] point_two = point_list[second_index] point_three = point_list[third_index] point_four = point_list[fourth_index] four_points = [point_one, point_two, point_three, point_four] four_points.sort(key=lambda coord: (coord[0], coord[1])) p1, p2, p3, p4 = four_points[0], four_points[1], four_points[2], four_points[3] if p1[0] == p2[0] and p3[0] == p4[0] and p1[1] == p3[1] and p2[1] == p4[1] and p1[0] < p3[0] and p1[1] < p2[1]: current_width = p3[0] - p1[0] current_height = p2[1] - p1[1] current_area = current_width * current_height is_rectangle_empty = True # We check all other points to ensure no conflicting points lie inside or on the boundaries for check_index in range(total_points): if check_index in (first_index, second_index, third_index, fourth_index): continue other_point = point_list[check_index] if p1[0] <= other_point[0] <= p3[0] and p1[1] <= other_point[1] <= p2[1]: is_rectangle_empty = False break if is_rectangle_empty: if current_area > maximum_area_found: maximum_area_found = current_area # We return the largest clear rectangle area found, defaulting to negative one if none exist return maximum_area_foundTo find the largest possible rectangle formed by specific points, the optimal strategy is to systematically test pairs of points that could serve opposite corners of a rectangle. For each chosen pair, it quickly checks if there are any forbidden points trapped inside or on the edges that would ruin the shape.
Here's how the algorithm would work step-by-step:
class Solution:
def max_rectangle_area(self, point_collection: list[list[int]]) -> int:
maximum_area_found = -1
point_set = set(tuple(coordinate) for coordinate in point_collection)
# Iterate through every possible pair of points to evaluate as potential diagonal corners.
for point_index_i in range(len(point_collection)):
for point_index_j in range(point_index_i + 1, len(point_collection)):
bottom_left_x, bottom_left_y = point_collection[point_index_i]
top_right_x, top_right_y = point_collection[point_index_j]
if bottom_left_x >= top_right_x or bottom_left_y >= top_right_y:
continue
if (bottom_left_x, top_right_y) not in point_set or (top_right_x, bottom_left_y) not in point_set:
continue
contains_other_points = False
# Verify that no other points reside strictly inside or on the boundaries of this rectangle.
for candidate_point in point_collection:
candidate_x, candidate_y = candidate_point
if bottom_left_x <= candidate_x <= top_right_x and bottom_left_y <= candidate_y <= top_right_y:
if candidate_point != [bottom_left_x, bottom_left_y] and candidate_point != [top_right_x, top_right_y] and candidate_point != [bottom_left_x, top_right_y] and candidate_point != [top_right_x, bottom_left_y]:
contains_other_points = True
break
if not contains_other_points:
current_area = (top_right_x - bottom_left_x) * (top_right_y - bottom_left_y)
maximum_area_found = max(maximum_area_found, current_area)
# Return the largest valid rectangle area discovered, or negative one if none exist.
return maximum_area_found| Case | How to Handle |
|---|---|
| Fewer than 4 points provided in the input array | Return -1 immediately as at least 4 corners are required to form any rectangle. |
| Points with duplicate coordinates | Filter out duplicate points or use a set data structure during preprocessing to avoid redundant checks and incorrect inclusion validation. |
| Points containing negative coordinates and large extreme values | Use standard integer types with coordinate comparisons that correctly handle negative ranges and prevent coordinate-based underflow. |
| No valid rectangle can be formed due to interior or boundary point constraints | Iterate through all valid pairs of diagonal points, verify the absence of other points inside or on the boundary, and return -1 if none pass. |
| Collinear points forming zero-area degenerate rectangles | Ensure rectangle width and height calculations strictly enforce positive non-zero dimensions before computing area. |
| Points forming valid rectangles that contain other points strictly inside or on their boundaries | Perform a secondary validation pass over all given points to ensure no other point falls inclusively within the bounding box of the candidate rectangle. |
| Large input scale leading to time limit exceeded due to O(N^3) or worse complexity | Optimize the search by hashing point coordinates for O(1) lookup and iterating intelligently over potential diagonal pairs. |
| Integer overflow during area calculation with very large coordinates | Use 64-bit integers to store and compare products of coordinate differences to prevent arithmetic overflow. |