Largest Triangle Area - Problem

Given an array of points on the X-Y plane points where points[i] = [xi, yi], return the area of the largest triangle that can be formed by any three different points.

Answers within 10-5 of the actual answer will be accepted.

Input & Output

Example 1 — Basic Triangle
$ Input: points = [[0,0],[0,1],[1,0],[2,1],[2,2]]
Output: 1.5
💡 Note: The largest triangle is formed by points [0,1], [1,0], [2,2] with area = |0(0-2) + 1(2-1) + 2(1-0)| / 2 = |0 + 1 + 2| / 2 = 1.5
Example 2 — Minimum Case
$ Input: points = [[1,0],[0,0],[0,1]]
Output: 0.5
💡 Note: Only one triangle possible with points forming a right triangle, area = 0.5 * 1 * 1 = 0.5
Example 3 — Collinear Points
$ Input: points = [[0,0],[1,1],[2,2],[3,4]]
Output: 1.0
💡 Note: Points [0,0], [1,1], [2,2] are collinear (area = 0), largest triangle uses [0,0], [2,2], [3,4] with area = |0(2-4) + 2(4-0) + 3(0-2)| / 2 = |0 + 8 - 6| / 2 = 1.0

Constraints

  • 3 ≤ points.length ≤ 50
  • -50 ≤ xi, yi ≤ 50
  • All the given points are unique.

Visualization

Tap to expand
Largest Triangle Area Optimized Brute Force Approach INPUT 0 1 2 0 1 2 A B C D E Points Array: [0,0] [0,1] [1,0] [2,1] [2,2] 5 points on X-Y plane A, B, C, D, E ALGORITHM STEPS 1 Generate Triplets All combinations of 3 points C(5,3) = 10 triplets 2 Calculate Area Use Shoelace formula: Area = 0.5 * |x1(y2-y3) + x2(y3-y1) + x3(y1-y2)| 3 Track Maximum Compare with max area maxArea = max(maxArea, area) 4 Return Result Output largest area found Example: Points B,D,E (0,1), (2,1), (2,2) Area = 0.5*|0+2+(-2)| = 1.0 FINAL RESULT (0,0) (2,2) (2,1) Output 2.0 Triangle: A, D, E (0,0), (2,1), (2,2) OK - Largest area = 2.0 Key Insight: The Shoelace formula computes triangle area directly from coordinates without needing side lengths or heights. For n points, we check C(n,3) = n*(n-1)*(n-2)/6 triangles. Time Complexity: O(n^3) | Space Complexity: O(1) TutorialsPoint - Largest Triangle Area | Optimized Brute Force
Asked in
Amazon 15 Google 12
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen