Dados dos lados de un triángulo s1 y s2 , la tarea es encontrar la longitud mínima y máxima posible del tercer lado del triángulo dado. Imprime -1 si no es posible hacer un triángulo con las longitudes de lado dadas. Tenga en cuenta que la longitud de todos los lados deben ser números enteros.
Ejemplos:
Entrada: s1 = 3, s2 = 6
Salida:
Max = 8
Min = 4
Entrada: s1 = 5, s2 = 8
Salida:
Max = 12
Min = 4
Enfoque: Sean s1 , s2 y s3 los lados del triángulo dado donde se dan s1 y s2 . Como sabemos que en un triángulo, la suma de dos lados siempre debe ser mayor que el tercer lado. Por lo tanto, se deben satisfacer las siguientes ecuaciones:
- s1 + s2 > s3
- s1 + s3 > s2
- s2 + s3 > s1
Resolviendo para s3 , obtenemos s3 < s1 + s2 , s3 > s2 – s1 y s3 > s1 – s2 .
Ahora está claro que la longitud del tercer lado debe estar en el rango (max(s1, s2) – min(s1, s2), s1 + s2)
Entonces, el valor mínimo posible será max(s1, s2) – min(s1, s2) + 1 y el valor máximo posible será s1 + s2 – 1 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <iostream> 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
// 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
# 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#
// 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..
Javascript
<script> // javascript implementation of the approach // Function to find the minimum and the // maximum possible length of the third // side of the given triangle function find_length(s1 , s2) { // Not a valid triangle if (s1 <= 0 || s2 <= 0) { document.write(-1); return; } var max_length = s1 + s2 - 1; var min_length = Math.max(s1, s2) - Math.min(s1, s2) + 1; // Not a valid triangle if (min_length > max_length) { document.write(-1); return; } document.write("Max = " + max_length+"<br/>"); document.write("Min = " + min_length); } // Driver code var s1 = 8, s2 = 5; find_length(s1, s2); // This code is contributed by todaysgaurav </script>
Max = 12 Min = 4
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)