There are n points on an infinite plane. You are given two integer arrays xCoord and yCoord where (xCoord[i], yCoord[i]) represents the coordinates of the ith point.
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: xCoord = [1,1,3,3], yCoord = [1,3,1,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: xCoord = [1,1,3,3,2], yCoord = [1,3,1,3,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: xCoord = [1,1,3,3,1,3], yCoord = [1,3,1,3,2,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 <= xCoord.length == yCoord.length <= 2 * 1050 <= xCoord[i], yCoord[i] <= 8 * 107When 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 collection of points scattered across a large grid and you want to find the largest possible rectangle that can be formed using four of these points as corners, without any other points trapped inside or on its borders. The brute force strategy involves testing every single group of four points to see if they form a valid rectangle.
Here's how the algorithm would work step-by-step:
class Solution:
def maxRectangleArea(self, points: list[list[int]]) -> int:
max_area_found = -1
total_points = len(points)
# Iterate through all combinations of four distinct points to test every possible rectangle
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 = points[first_index]
point_two = points[second_index]
point_three = points[third_index]
point_four = points[fourth_index]
all_points_group = [point_one, point_two, point_three, point_four]
sorted_by_x = sorted(all_points_group, key=lambda coord: (coord[0], coord[1]))
if sorted_by_x[0][0] == sorted_by_x[1][0] and sorted_by_x[2][0] == sorted_by_x[3][0]:
if sorted_by_x[0][1] == sorted_by_x[2][1] and sorted_by_x[1][1] == sorted_by_x[3][1]:
min_x = sorted_by_x[0][0]
max_x = sorted_by_x[2][0]
min_y = sorted_by_x[0][1]
max_y = sorted_by_x[1][1]
if min_x == max_x or min_y == max_y:
continue
point_is_inside = False
# Check all remaining points in the grid to ensure no point violates the empty rectangle constraint
for check_index in range(total_points):
if check_index in (first_index, second_index, third_index, fourth_index):
continue
candidate_x, candidate_y = points[check_index]
if min_x <= candidate_x <= max_x and min_y <= candidate_y <= max_y:
point_is_inside = True
break
if not point_is_inside:
current_width = max_x - min_x
current_height = max_y - min_y
current_area = current_width * current_height
# Keep track of the largest valid area found so far among all tested empty rectangles
if current_area > max_area_found:
max_area_found = current_area
return max_area_foundInstead of checking every single possible pair of points to form a rectangle, the optimal approach organizes the points by their vertical and horizontal alignments. It then systematically searches for valid opposite corners while quickly verifying that no forbidden points are trapped inside.
Here's how the algorithm would work step-by-step:
class Solution:
def max_rectangle_area(self, points: list[list[int]]) -> int:
from collections import defaultdict
import bisect
point_set = set((x_coord, y_coord) for x_coord, y_coord in points)
vertical_groups = defaultdict(list)
for x_coord, y_coord in points:
vertical_groups[x_coord].append(y_coord)
for x_coord in vertical_groups:
vertical_groups[x_coord].sort()
# Grouping by x-coordinate allows us to efficiently find vertical segments.
sorted_x_coords = sorted(vertical_groups.keys())
segment_tracker = {}
maximum_area = -1
for current_x in sorted_x_coords:
y_list = vertical_groups[current_x]
total_y_points = len(y_list)
for first_index in range(total_y_points):
for second_index in range(first_index + 1, total_y_points):
bottom_y = y_list[first_index]
top_y = y_list[second_index]
interval_pair = (bottom_y, top_y)
# Tracking previous spans lets us instantly identify matching rectangle heights.
if interval_pair in segment_tracker:
previous_x = segment_tracker[interval_pair]
width = current_x - previous_x
height = top_y - bottom_y
current_area = width * height
# Checking interior points ensures no forbidden coordinates violate the rectangle.
is_valid_rectangle = True
for checked_x in sorted_x_coords:
if previous_x < checked_x < current_x:
idx_lower = bisect.bisect_left(vertical_groups[checked_x], bottom_y)
idx_upper = bisect.bisect_right(vertical_groups[checked_x], top_y)
if idx_upper - idx_lower > 0:
is_valid_rectangle = False
break
if is_valid_rectangle:
for checked_x in [previous_x, current_x]:
idx_lower = bisect.bisect_left(vertical_groups[checked_x], bottom_y)
idx_upper = bisect.bisect_right(vertical_groups[checked_x], top_y)
if idx_upper - idx_lower != 2:
is_valid_rectangle = False
break
if is_valid_rectangle:
maximum_area = max(maximum_area, current_area)
segment_tracker[interval_pair] = current_x
# Maintaining the max area throughout guarantees we return the optimal solution.
return maximum_area| Case | How to Handle |
|---|---|
| Fewer than 4 points provided in the input set | Return the specific indicator for no rectangle since at least 4 vertices are required to form a rectangle. |
| Points forming a rectangle but containing other points strictly inside or on the boundary | Filter out candidate rectangles by verifying that the bounding box defined by the diagonal points contains zero other points. |
| Points with negative coordinates or large coordinate values up to 10^9 | Use coordinate compression or hash maps with coordinate pairs to safely index and compare points without out-of-bounds issues. |
| Duplicate points in the input set | Deduplicate the input array initially using a set to ensure unique vertex locations and prevent degenerate rectangle checks. |
| No valid rectangles exist under the point constraints | Return -1 or the designated sentinel value indicating that no valid empty rectangle could be formed. |
| Extremely large number of points scaling towards the maximum constraint limit | Optimize the search by grouping points by x or y coordinates and utilizing sweep-line algorithms or interval trees for efficient range queries. |
| Large coordinate products resulting in integer overflow when calculating area | Perform all area calculations using 64-bit integer types to prevent overflow during multiplication of high coordinate differences. |
| Points forming valid rectangles that share overlapping edges or are nested within each other | Independently evaluate all valid pairs of diagonal points and only retain the maximum calculated area that satisfies the empty constraint. |