#812
Easy Algorithms Largest triangle area
Array Math Geometry
71.5% acceptance
Feb 22, 2026
844
1739
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.
Solution
Rust
Time O(n³)
Space O(1)
/*
* 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.
* Example 1:
* Input: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
* Output: 2.00000
* Explanation: The five points are shown in the above figure. The red triangle is the largest.
* Example 2:
* Input: points = [[1,0],[0,0],[0,1]]
* Output: 0.50000
* Constraints:
* 3 <= points.length <= 50
* -50 <= xi, yi <= 50
* All the given points are unique.
*/
impl Solution {
pub fn largest_triangle_area(points: Vec<Vec<i32>>) -> f64 {
let n = points.len();
let mut ans = 0f64;
for i in 0..n {
for j in i+1..n {
for k in j+1..n {
let (ax,ay) = (points[i][0] as f64, points[i][1] as f64);
let (bx,by) = (points[j][0] as f64, points[j][1] as f64);
let (cx,cy) = (points[k][0] as f64, points[k][1] as f64);
let area = ((ax*(by-cy) + bx*(cy-ay) + cx*(ay-by))/2.0).abs();
ans = ans.max(area);
}
}
}
ans
}
}