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.
Example 1:
Input: sides = [3,4,5]
Output: [36.86990,53.13010,90.00000]
Explanation:
You can form a right-angled triangle with side lengths 3, 4, and 5. The internal angles of this triangle are approximately 36.869897646, 53.130102354, and 90 degrees respectively.
Example 2:
Input: sides = [2,4,2]
Output: []
Explanation:
You cannot form a triangle with positive area using side lengths 2, 4, and 2.
Constraints:
sides.length == 31 <= sides[i] <= 1000Problem Overview: You are given three integer angles. The task is to determine whether these angles can form a valid triangle. A triangle is valid only when all angles are positive and their sum equals 180 degrees.
Approach 1: Direct Angle Sum Check (O(1) time, O(1) space)
The simplest solution uses the fundamental rule from math and geometry: the sum of the three interior angles of a triangle must be exactly 180. Compute a + b + c and verify that it equals 180. Also ensure each angle is greater than 0, since a triangle cannot contain zero or negative angles. This approach performs a constant number of arithmetic and comparison operations, so the runtime and memory usage are both constant.
Approach 2: Compute the Third Angle (O(1) time, O(1) space)
Another way is to treat the triangle rule as c = 180 - (a + b). First check that a and b are positive, then compute the required third angle. If the computed value equals the provided c and is also positive, the triangle is valid. This method mirrors how triangle problems often appear in interviews where two angles are known and the third is derived. Internally it performs the same constant-time arithmetic but emphasizes the geometric relationship between the angles.
Approach 3: Iterative Validation with an Array (O(1) time, O(1) space)
If the angles are stored in a container (for example an array or list), iterate once to verify each value is positive while accumulating the total sum. After the loop, check whether the sum equals 180. This structure is useful when the input format changes or when triangle properties are processed inside a larger simulation or validation pipeline. Even though it uses a loop, the number of elements is fixed at three, so the complexity remains constant.
Recommended for interviews: The direct angle sum check is the cleanest and most expected solution. It demonstrates that you know the triangle angle rule and can translate it into a constant-time validation. Showing the iterative or derived-angle variant can help explain your reasoning, but the optimal answer is the single-pass arithmetic check with a + b + c == 180 and positive angles.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Angle Sum Check | O(1) | O(1) | Best general solution when three angles are directly provided |
| Compute the Third Angle | O(1) | O(1) | Useful when two angles determine the third in geometric reasoning problems |
| Iterative Validation with Array | O(1) | O(1) | When angles are stored in a list or processed in a validation pipeline |
Practice Angles of a Triangle with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor