[LeetCode] Angles of a Triangle

3899. Angles of a Triangle

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<double> internalAngles(vector<int>& sides) {
sort(sides.begin(), sides.end());
double a = sides[0], b = sides[1], c = sides[2];
if (a + b <= c) return {};

const double PI = acos(-1.0);
auto getAngle = [&](double x, double y, double z) {
double v = (y * y + z * z - x * x) / (2.0 * y * z);
v = max(-1.0, min(1.0, v));
return acos(v) * 180.0 / PI;
};

vector<double> res = {
getAngle(a, b, c),
getAngle(b, a, c),
getAngle(c, a, b)
};
sort(res.begin(), res.end());
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/angles-of-a-triangle/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.