Given two sides of a triangle s1 and s2, the task is to find the minimum and maximum possible length of the third side of the given triangle. Print -1 if it is not possible to make a triangle with the given side lengths. Note that the length of all the sides must be integers.
Input: s1 = 3, s2 = 6
Max = 8
Min = 4
Input: s1 = 5, s2 = 8
Max = 12
Min = 4
Approach: Let s1, s2 and s3 be the sides of the given triangle where s1 and s2 are given. As we know that in a triangle, the sum of two sides must always be greater than the third side. So, the following equations must be satisfied:
- s1 + s2 > s3
- s1 + s3 > s2
- s2 + s3 > s1
Solving for s3, we get s3 < s1 + s2, s3 > s2 – s1 and s3 > s1 – s2.
It is clear now that the length of the third side must lie in the range (max(s1, s2) – min(s1, s2), s1 + s2)
So, the minimum possible value will be max(s1, s2) – min(s1, s2) + 1 and the maximum possible value will be s1 + s2 – 1.
Below is the implementation of the above approach:
Max = 12 Min = 4
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.
- Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
- Length of remaining two sides of a Triangle from a given side and its adjacent angles
- Find Maximum side length of square in a Matrix
- Minimum length of the shortest path of a triangle
- Find coordinates of the triangle given midpoint of each side
- Find the length of the median of a Triangle if length of sides are given
- Count squares with odd side length in Chessboard
- Area of a n-sided regular polygon with given side length
- Number of squares of side length required to cover an N*M rectangle
- Minimum side of square embedded in Regular polygon with N sides
- Length of longest subarray of length at least 2 with maximum GCD
- Maximum length Subsequence with alternating sign and maximum Sum
- Maximum path sum in an Inverted triangle | SET 2
- Maximum Perimeter Triangle from array
- Maximum of all the integers in the given level of Pascal triangle
- Maximum area of rectangle inscribed in an equilateral triangle
- Minimum adjacent swaps to move maximum and minimum to corners
- Minimum distance between the maximum and minimum element of a given Array
- Minimum LCM of all subarrays of length at least 2
- Maximum GCD of all subarrays of length at least 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.