You are given a list of points on a flat grid, each written as [x, y]. Choose any three different points as the corners of a triangle.
Return the largest area such a triangle can have. Answers within 0.00001 of the true area count as correct.
Example 1
Input: points = [[0, 0], [4, 0], [1, 3], [2, 1]] Output: 6.0 Why: base 4 along the x-axis and height 3 give 4 · 3 / 2
2 more examples and the constraints
Example 2
Input: points = [[-2, 0], [0, 0], [2, 0], [0, -1]] Output: 2.0 Why: the outer two points make a base of 4; height 1
Example 3
Input: points = [[2, 1], [5, 1], [2, 6]] Output: 7.5 Why: a right triangle with legs 3 and 5
Constraints
- 3 ≤ number of points ≤ 50
- −50 ≤ x, y ≤ 50
- No two points are the same
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Every triple, Heron’s formulaO(n³)
- Every triple, cross productO(n³)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 50 points | Verdict |
|---|---|---|---|
| Heron’s formula | O(n³) | Passes; square roots and rounding | |
| Cross product | O(n³) | Best; exact integer math |
n = number of points. Both try 19,600 triples; the cross product does less work for each.
Edge cases to test
- All points on one line. Every triangle is flat, so the answer is 0.
- Exactly three points. There is only one triangle to measure.
- Coordinates at the limits. Corners of the 100 × 100 square give 5,000; the doubled area, 10,000, still fits an int.
- Negative cross product. The sign depends on corner order; take the absolute value.
Common mistakes
- Forgetting the absolute value, so a clockwise triangle counts as negative.
- Dividing by 2 with integer division and losing the .5.
- Using Heron’s formula without clamping, then taking the square root of a tiny negative.
How to talk through it
- 1Clarify. Are the points distinct? How exact must the answer be?
- 2Size it up. 50 points give under 20,000 triples, so trying them all is fine.
- 3Pick the formula. Half the cross product of two edges: exact, no square roots.
- 4Code the triple loop. Use i < j < k so each triangle is tried once.
- 5Test out loud. Run three points on a line and a right triangle with an odd area.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?