Skip to main content
Back to problems
#3899
Medium Algorithms

Angles of a triangle

62.1% acceptance
May 13, 2026
32
36
You are given a positive integer array sides of length 3. Determine if there exists a triangle with positive area whose three side lengths are given by the elements of sides. If such a triangle exists, return an array of three floating-point numbers representing its internal angles (in degrees), sorted in non-decreasing order. Otherwise, return an empty array. Answers within 10-5 of the actual answer will be accepted.

Solution

Rust
Time O(n log n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn internal_angles(sides: Vec<i32>) -> Vec<f64> {
    let mut s = sides.clone();
    s.sort_unstable();
    let a = s[0] as f64;
    let b = s[1] as f64;
    let c = s[2] as f64;
    if a + b <= c {
      return vec![];
    }
    let to_deg = 180.0 / std::f64::consts::PI;
    let cos_a = (b * b + c * c - a * a) / (2.0 * b * c);
    let cos_b = (a * a + c * c - b * b) / (2.0 * a * c);
    let angle_a = cos_a.acos() * to_deg;
    let angle_b = cos_b.acos() * to_deg;
    let angle_c = 180.0 - angle_a - angle_b;
    vec![angle_a, angle_b, angle_c]
  }
}