# Minimum and maximum possible length of the third side of a triangle

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.

Examples:

Input: s1 = 3, s2 = 6
Output:
Max = 8
Min = 4

Input: s1 = 5, s2 = 8
Output:
Max = 12
Min = 4

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

1. s1 + s2 > s3
2. s1 + s3 > s2
3. 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:

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the minimum and the ` `// maximum possible length of the third ` `// side of the given triangle ` `void` `find_length(``int` `s1, ``int` `s2) ` `{ ` ` `  `    ``// Not a valid triangle ` `    ``if` `(s1 <= 0 || s2 <= 0) { ` `        ``cout << -1; ` `        ``return``; ` `    ``} ` `    ``int` `max_length = s1 + s2 - 1; ` `    ``int` `min_length = max(s1, s2) - min(s1, s2) + 1; ` ` `  `    ``// Not a valid triangle ` `    ``if` `(min_length > max_length) { ` `        ``cout << -1; ` `        ``return``; ` `    ``} ` ` `  `    ``cout << ``"Max = "` `<< max_length << endl; ` `    ``cout << ``"Min = "` `<< min_length; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `s1 = 8, s2 = 5; ` `    ``find_length(s1, s2); ` ` `  `    ``return` `0; ` `} `

 `// Java implementation of the approach ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to find the minimum and the ` `// maximum possible length of the third ` `// side of the given triangle ` `static` `void` `find_length(``int` `s1, ``int` `s2) ` `{ ` ` `  `    ``// Not a valid triangle ` `    ``if` `(s1 <= ``0` `|| s2 <= ``0``) ` `    ``{ ` `        ``System.out.print(-``1``); ` `        ``return``; ` `    ``} ` `    ``int` `max_length = s1 + s2 - ``1``; ` `    ``int` `min_length = Math.max(s1, s2) - Math.min(s1, s2) + ``1``; ` ` `  `    ``// Not a valid triangle ` `    ``if` `(min_length > max_length)  ` `    ``{ ` `        ``System.out.print(-``1``); ` `        ``return``; ` `    ``} ` ` `  `    ``System.out.println(``"Max = "` `+ max_length); ` `    ``System.out.print(``"Min = "` `+ min_length); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `s1 = ``8``, s2 = ``5``; ` `    ``find_length(s1, s2); ` `} ` `} ` ` `  `// This code is contributed by anuj_67.. `

 `# Python3 implementation of the approach  ` ` `  `# Function to find the minimum and the  ` `# maximum possible length of the third  ` `# side of the given triangle  ` `def` `find_length(s1, s2) :  ` ` `  `    ``# Not a valid triangle  ` `    ``if` `(s1 <``=` `0` `or` `s2 <``=` `0``) :  ` `        ``print``(``-``1``, end ``=` `"");  ` `        ``return``;  ` `         `  `    ``max_length ``=` `s1 ``+` `s2 ``-` `1``;  ` `    ``min_length ``=` `max``(s1, s2) ``-` `min``(s1, s2) ``+` `1``;  ` ` `  `    ``# Not a valid triangle  ` `    ``if` `(min_length > max_length) : ` `        ``print``(``-``1``, end ``=` `"");  ` `        ``return``;  ` ` `  `    ``print``(``"Max ="``, max_length);  ` `    ``print``(``"Min ="``, min_length);  ` ` `  ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``s1 ``=` `8``; ` `    ``s2 ``=` `5``; ` `     `  `    ``find_length(s1, s2);  ` `     `  `# This code is contributed by AnkitRai01 `

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to find the minimum and the ` `// maximum possible length of the third ` `// side of the given triangle ` `static` `void` `find_length(``int` `s1, ``int` `s2) ` `{ ` ` `  `    ``// Not a valid triangle ` `    ``if` `(s1 <= 0 || s2 <= 0) ` `    ``{ ` `        ``Console.Write(-1); ` `        ``return``; ` `    ``} ` `    ``int` `max_length = s1 + s2 - 1; ` `    ``int` `min_length = Math.Max(s1, s2) - Math.Min(s1, s2) + 1; ` ` `  `    ``// Not a valid triangle ` `    ``if` `(min_length > max_length)  ` `    ``{ ` `        ``Console.WriteLine(-1); ` `        ``return``; ` `    ``} ` ` `  `    ``Console.WriteLine(``"Max = "` `+ max_length); ` `    ``Console.WriteLine(``"Min = "` `+ min_length); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main ()  ` `{ ` `    ``int` `s1 = 8, s2 = 5; ` `    ``find_length(s1, s2); ` `} ` `} ` ` `  `// This code is contributed by anuj_67.. `

Output:
```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.

Article Tags :
Practice Tags :