You are given a 2D array coords of size n x 2, representing the coordinates of n points in an infinite Cartesian plane.
Find twice the maximum area of a triangle with its corners at any three elements from coords, such that at least one side of this triangle is parallel to the x-axis or y-axis. Formally, if the maximum area of such a triangle is A, return 2 * A.
If no such triangle exists, return -1.
Note that a triangle cannot have zero area.
Example 1:
Input: coords = [[1,1],[1,2],[3,2],[3,3]]
Output: 2
Explanation:

The triangle shown in the image has a base 1 and height 2. Hence its area is 1/2 * base * height = 1.
Example 2:
Input: coords = [[1,1],[2,2],[3,3]]
Output: -1
Explanation:
The only possible triangle has corners (1, 1), (2, 2), and (3, 3). None of its sides are parallel to the x-axis or the y-axis.
Constraints:
1 <= n == coords.length <= 1051 <= coords[i][0], coords[i][1] <= 106coords[i] are unique.When 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:
The brute force approach to finding the maximum triangle area is like trying every single combination of three points to see which one makes the biggest triangle. We calculate the area for each possible triangle and keep track of the largest area we've seen so far.
Here's how the algorithm would work step-by-step:
def find_maximum_area_of_a_triangle(points):
number_of_points = len(points)
maximum_area = 0
# Iterate through all possible combinations of three points.
for first_point_index in range(number_of_points):
for second_point_index in range(first_point_index + 1, number_of_points):
for third_point_index in range(second_point_index + 1, number_of_points):
# Get the three points for the current triangle
point_one = points[first_point_index]
point_two = points[second_point_index]
point_three = points[third_point_index]
# Calculate the area of the triangle using the determinant method.
area = 0.5 * abs(
point_one[0] * (point_two[1] - point_three[1]) +
point_two[0] * (point_three[1] - point_one[1]) +
point_three[0] * (point_one[1] - point_two[1])
)
# Keep track of the largest area found
if area > maximum_area:
maximum_area = area
return maximum_area
The goal is to find the largest triangle area formed by any three points from a given set of points. We avoid checking every possible triangle by cleverly using geometry to cut down the work we need to do. The key idea is to use the rotating calipers technique combined with the convex hull, a shape that encloses all the points.
Here's how the algorithm would work step-by-step:
def find_maximum_area_of_triangle(points):
def cross_product(point1, point2, point3):
return (point2[0] - point1[0]) * (point3[1] - point1[1]) - (point2[1] - point1[1]) * (point3[0] - point1[0])
def calculate_area(point1, point2, point3):
return 0.5 * abs(cross_product(point1, point2, point3))
def convex_hull(points):
points = sorted(points)
if len(points) <= 1:
return points
upper_hull = []
for point in points:
while len(upper_hull) >= 2 and cross_product(upper_hull[-2], upper_hull[-1], point) <= 0:
upper_hull.pop()
upper_hull.append(point)
lower_hull = []
for point in reversed(points):
while len(lower_hull) >= 2 and cross_product(lower_hull[-2], lower_hull[-1], point) <= 0:
lower_hull.pop()
lower_hull.append(point)
return upper_hull[:-1] + lower_hull[:-1]
convex_hull_points = convex_hull(points)
number_of_hull_points = len(convex_hull_points)
if number_of_hull_points < 3:
return 0
maximum_area = 0
for i in range(number_of_hull_points):
left_pointer = (i + 1) % number_of_hull_points
right_pointer = (i + 2) % number_of_hull_points
# Iterate through all possible combinations to maximize area.
while True:
area1 = calculate_area(convex_hull_points[i], convex_hull_points[left_pointer], convex_hull_points[right_pointer])
next_right_pointer = (right_pointer + 1) % number_of_hull_points
area2 = calculate_area(convex_hull_points[i], convex_hull_points[left_pointer], convex_hull_points[next_right_pointer])
# Move right pointer if it leads to a bigger area.
if area2 > area1:
right_pointer = next_right_pointer
else:
maximum_area = max(maximum_area, area1)
break
return maximum_area| Case | How to Handle |
|---|---|
| Empty input list | Return 0, as no triangle can be formed. |
| Input list with fewer than 3 points | Return 0, as a triangle requires at least 3 points. |
| All points are collinear (lie on the same line) | Return 0, as a degenerate triangle has zero area. |
| Duplicate points in the input list | The area calculation formula will still work correctly; duplicates should not affect the maximum area found if they do not constitute one of the vertices of the maximum area triangle. |
| Integer overflow during area calculation with large coordinate values | Use a data type capable of holding larger values (e.g., long) or floating-point numbers (e.g., double) for the area calculation. |
| Floating-point precision issues when calculating area | Use a small tolerance value when comparing areas to account for potential floating-point inaccuracies. |
| Input list contains points with very large absolute coordinate values | Ensure that the chosen data types (e.g., int, long, double) can accommodate the range of coordinate values without overflow or loss of precision. |
| The three points that form the largest area triangle are close to being collinear | Floating-point imprecision might affect which triangle is identified, but should generally converge to correct area. |