Find Maximum Area of a Triangle

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
Inputcoords = [[1,1],[1,2],[3,2],[3,3]]
Output2
The triangle has a base of 1 and height of 2, so its area is 1 and twice the area is 2.
Example 2
Inputcoords = [[1,1],[2,2],[3,3]]
Output-1
The only possible triangle uses all three points, and none of its sides are parallel to the x-axis or the y-axis.

Constraints

  • 1 <= n == coords.length <= 10^5
  • 1 <= coords[i][0], coords[i][1] <= 10^6
  • All coords[i] are unique.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate