LeetCode Problem

How to Solve Find Maximum Area of a Triangle

To solve this problem, identify the three points that form the maximum area triangle, ensuring one side is aligned with the axes. Use array scanning and hash lookup techniques to efficiently calculate the area of potential triangles and return twice the maximum area, or -1 if no such triangle exists.

GhostInterview Help

Need help with Find Maximum Area of a Triangle without spending extra time grinding it?

GhostInterview can read Find Maximum Area of a Triangle from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3588Array scanning plus hash lookupReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve this problem, identify the three points that form the maximum area triangle, ensuring one side is aligned with the axes. Use array scanning and hash lookup techniques to efficiently calculate the area of potential triangles and return twice the maximum area, or -1 if no such triangle exists.

Problem Statement

You are given a 2D array coords representing coordinates of n points in a Cartesian plane. The task is to find the maximum area of a triangle that can be formed using any three points from coords such that at least one side of the triangle is parallel to the x-axis or y-axis. If no such triangle exists, return -1.

The area should be returned as twice the maximum possible area of such a triangle. The solution should involve scanning the coordinates and looking for the largest valid triangle formed by the given points.

Examples

Example 1

Input: coords = [[1,1],[1,2],[3,2],[3,3]]

Output: 2

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

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 <= 105
  • 1 <= coords[i][0], coords[i][1] <= 106
  • All coords[i] are unique.

Solution Approach

Array Scanning and Hash Lookup

The primary technique for solving this problem involves scanning the coordinates and using a hash table to track the x and y coordinates. By leveraging this approach, we can efficiently determine which points can form a valid triangle where one side is aligned with the x-axis or y-axis.

Geometric Area Calculation

Once the valid points are identified, calculate the area of the triangle using the formula Area = (base * height) / 2. To avoid unnecessary floating-point calculations, return twice the area directly.

Edge Case Handling

Handle edge cases such as when no valid triangles can be formed. For example, if all points are collinear or there are fewer than three points, return -1 as specified by the problem statement.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity depends on the method used for scanning the points and performing hash lookups, potentially O(n). Space complexity may also be O(n) due to the need for storing the coordinates and hash data.

What Interviewers Usually Probe

  • Ability to optimize the search for valid triangles using hash lookups.
  • Understanding of geometry concepts for calculating the area of a triangle.
  • Skill in handling edge cases and ensuring the solution scales for large inputs.

Common Pitfalls or Variants

Common pitfalls

  • Failing to handle cases where no valid triangle can be formed, leading to incorrect results.
  • Using inefficient methods that result in excessive time complexity.
  • Overlooking the need to double the area for the final output.

Follow-up variants

  • Allowing different coordinate systems or constraints, like a fixed boundary for the plane.
  • Considering the maximum number of points (n) for performance optimization.
  • Extending to 3D coordinates where the problem becomes more complex.

How GhostInterview Helps

  • GhostInterview helps you focus on the key areas of the problem, ensuring you approach it from an optimal perspective.
  • The platform suggests efficient data structures, like hash tables, and guides you in avoiding unnecessary computational overhead.
  • GhostInterview provides detailed insights into how geometry and array scanning can be effectively combined for fast and accurate solutions.

Topic Pages

FAQ

What is the approach to solving the 'Find Maximum Area of a Triangle' problem?

The approach combines array scanning with hash lookup to efficiently identify valid points and calculate the area of a potential triangle, ensuring one side is parallel to an axis.

Why must the area be returned as twice the value?

The problem specifically asks to return twice the maximum area of a triangle that meets the criteria of having one side parallel to the axes.

What are the main difficulties in solving this problem?

The primary challenges are efficiently scanning the coordinates, handling large inputs, and ensuring that the triangle sides are correctly aligned with the axes.

How does the hash lookup optimize solving this problem?

Hash lookup allows for faster identification of points that can form valid triangles, avoiding a brute-force approach and reducing computational complexity.

What happens if no valid triangle can be formed?

If no valid triangle is found, the problem specifies that the function should return -1 as the result.

GhostInterview Solver

Need direct help with Find Maximum Area of a Triangle instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Find Maximum Area of a Triangle from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.