Skip to main content

Angles of a Triangle - Solution & Explanation

MediumArrayMathGeometry8 min read
Practice this problem

Problem Statement

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 == 3
  • 1 <= sides[i] <= 1000

Approach Overview

Problem 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.

Solution

We first sort the array sides in non-decreasing order, and denote the three side lengths as a, b, and c, where a \le b \le c.

According to the triangle inequality, if a + b \le c, then these three sides cannot form a triangle with positive area, so we return an empty array directly.

Otherwise, the three sides can form a valid triangle. By the law of cosines, we have:

$ \cos A = \frac{b^2 + c^2 - a^2}{2bc}

\cos B = \frac{a^2 + c^2 - b^2}{2ac}

Therefore, we can compute angles A and B separately. Finally, using the fact that the sum of the internal angles of a triangle is 180^\circ, we get:

C = 180^\circ - A - B

Finally, we return the three internal angles.

The time complexity is O(1), and the space complexity is O(1)$.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct Angle Sum CheckO(1)O(1)Best general solution when three angles are directly provided
Compute the Third AngleO(1)O(1)Useful when two angles determine the third in geometric reasoning problems
Iterative Validation with ArrayO(1)O(1)When angles are stored in a list or processed in a validation pipeline

Video Solution

Angles of a Triangle | LeetCode XXXX | Weekly Contest 497 | Java Code | Developer CoderDeveloper Coder130 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Angles of a Triangle easy or hard?
This problem is generally considered easy to medium depending on context. The core logic is simple geometry, but interviewers may include additional constraints or validations that require careful handling of edge cases such as zero or negative angles.
Angles of a Triangle Python/Java solution
The implementation in Python, Java, or C++ is identical in logic: check that all angles are positive and that their sum equals 180. Since the algorithm is constant time, the code usually consists of a few conditional statements and a single addition operation.
How to solve Angles of a Triangle in O(1)?
Add the three angles and verify that the result equals 180. Also ensure each angle is greater than zero. Because the number of operations does not depend on input size, the algorithm executes in constant time and constant space.
What is the best approach for Angles of a Triangle?
The best approach is a direct mathematical validation using the triangle angle rule. Check that all three angles are positive and that their sum equals 180 degrees. This solution runs in O(1) time and uses O(1) space because it only performs a few arithmetic and comparison operations.
Is Angles of a Triangle asked at Google/Amazon/Meta?
Triangle validation problems appear in coding screens and online assessments, especially in math or logic sections. While the exact problem title may vary, companies such as Amazon and Google frequently include similar constant-time validation or geometry-based questions.
What data structure is used in Angles of a Triangle?
No complex data structure is required. The problem typically uses simple integer variables or a small array/list containing the three angles. The logic relies on arithmetic validation rather than advanced structures like hash maps or trees.
What is the time complexity of Angles of a Triangle?
The optimal solution runs in O(1) time since the algorithm only evaluates three numbers and performs constant arithmetic checks. Space complexity is also O(1) because no additional data structures are required.

Ready to solve this problem?

Practice Angles of a Triangle with our built-in code editor and test cases.

Practice on FleetCode