Given the length of all three sides of a triangle as a, b and c. The task is to calculate the length of the median of the triangle.
A median of a triangle is a line segment joining a vertex to the midpoint of the opposite side, thus bisecting that side.
Input: a = 8, b = 10, c = 13
Input: a = 4, b = 3, c = 5
Approach: The idea is to use Apollonius’s theorem to solve this problem.
Apollonius’s Theorem states that “the sum of the squares of any two sides of a triangle equals twice the square on half the third side and twice the square on the median bisecting the third side”.
From the above figure, According to Apollonius’s Theorm we have:
where a, b, and c are the length of sides of the triangle
and m is the length of median of the triangle on side 2*a
Therefore, the length of the median of a triangle from the above equation is given by:
Below is the implementation of the above approach:
Time Complexity: O(1)
Space Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.