Angles of a Triangle
Angles of a Triangle solution for LeetCode 3899, with the key idea, complexity breakdown, and working code in Java, C++, JavaScript, TypeScript, C, Go, and Rust.
Solve on LeetCode ↗Angles of a Triangle
Problem summary
We are given exactly three side lengths in sides.
We need to:
- decide whether these three lengths can form a triangle with positive area
- if yes, return the three internal angles in degrees
- sort the angles in non-decreasing order
- otherwise return an empty array
Core idea
This is a direct geometry problem.
There are two steps:
- Check whether the three sides can form a valid triangle.
- If they can, compute the three angles using the Law of Cosines.
Triangle validity check
Three positive lengths form a triangle with positive area only if the triangle inequality holds:
a + b > c
b + c > a
c + a > b
If any one of these fails, the three points collapse into a line or cannot connect at all, so the answer is [].
Law of Cosines
If the triangle is valid, then:
cos(A) = (b^2 + c^2 - a^2) / (2bc)
cos(B) = (a^2 + c^2 - b^2) / (2ac)
cos(C) = (a^2 + b^2 - c^2) / (2ab)
So each angle can be found with acos(...).
That gives the angles in radians, and the problem asks for degrees, so we convert by:
degrees = radians * 180 / pi
Finally, we sort the three angles and return them.
Why this works
The triangle inequality tells us whether a non-degenerate triangle exists.
Once the triangle exists, the Law of Cosines uniquely determines each angle from the side lengths.
So the method is both complete and exact enough for the required precision.
Approach
- Read
a,b, andcfrom the input. - Check triangle inequality.
- If invalid, return an empty array.
- Otherwise compute
A,B, andCwithacos. - Convert them from radians to degrees.
- Sort and return.
Code Solution
Switch between languages
import java.util.Arrays;
class Solution {
public double[] internalAngles(int[] s) {
double a = s[0], b = s[1], c = s[2];
final double pie = 3.14159265358979323846;
if (a + b > c && b + c > a && c + a > b && a != 0 && b != 0 && c != 0) {
double A = Math.acos((b * b + c * c - a * a) / (2 * b * c));
double B = Math.acos((a * a + c * c - b * b) / (2 * a * c));
double C = Math.acos((a * a + b * b - c * c) / (2 * a * b));
A *= 180.0 / pie;
B *= 180.0 / pie;
C *= 180.0 / pie;
double[] ans = new double[] { A, B, C };
Arrays.sort(ans);
return ans;
}
return new double[0];
}
}Walkthrough
Take:
sides = [3, 4, 5]
First, check validity:
3 + 4 > 5
4 + 5 > 3
3 + 5 > 4
All are true, so a triangle exists.
Now compute the angles:
A = acos((4^2 + 5^2 - 3^2) / (2 * 4 * 5))
B = acos((3^2 + 5^2 - 4^2) / (2 * 3 * 5))
C = acos((3^2 + 4^2 - 5^2) / (2 * 3 * 4))
That gives approximately:
36.869897646
53.130102354
90.000000000
After sorting, the answer is:
[36.86990, 53.13010, 90.00000]
Complexity analysis
Time Complexity: O(1)
- there are only 3 sides
- all computations are constant work
Space Complexity: O(1)
- we only store a few floating-point values
Common mistakes
1. Forgetting triangle inequality
You should not apply the Law of Cosines blindly before checking validity.
2. Returning radians instead of degrees
acos returns radians, so a conversion step is required.
3. Not sorting the result
The problem asks for the angles in non-decreasing order.
Why I like this problem
This one is short, but it is a nice reminder that not every medium problem needs heavy data structures or DP.
Sometimes the real task is just:
- know the right math formula
- apply it carefully
- return the result in the required format
7 DP Patterns > 100 LeetCode Questions
Most DP questions are repeated ideas. Stop treating DP like chaos. Learn the 7 repeatable patterns that unlock most placement-level questions.