Dados tres lados del triángulo, encuentre el aumento mínimo en la longitud del lado del triángulo para que el área del triángulo no sea negativa.
Ejemplos:
Entrada : a = 3, b = 4, c = 10
Salida : 3
Con los lados dados, el área es negativa.
Si a aumenta a 5 yb a 5, entonces el área se convierte en 0, que no es negativo.
Entrada : a = 6, b = 6, c = 10
Salida : 2
Enfoque : dado que el área de cualquier triángulo no es negativa si la suma de los dos lados más pequeños siempre es mayor o igual que el tercer lado, se deben seguir los siguientes pasos para resolver el problema anterior:
- Ordena los tres lados en orden creciente.
- Comprueba si la suma de los dos primeros lados es mayor o igual que el tercer lado, si lo es, entonces la respuesta es 0.
- Si no es así, entonces la respuesta será (tercer lado – (primer lado + segundo lado)).
A continuación se muestra la implementación del enfoque dado.
C++
// C++ program to find Minimum // increase in sides to get // non-negative area of a triangle #include <bits/stdc++.h> using namespace std; // Function to return the minimum increase in side // lengths of the triangle int minimumIncrease(int a, int b, int c) { // push the three sides to a array int arr[] = { a, b, c }; // sort the array sort(arr, arr + 3); // check if sum is greater than third side if (arr[0] + arr[1] >= arr[2]) return 0; else return arr[2] - (arr[0] + arr[1]); } // Driver Code int main() { int a = 3, b = 5, c = 10; cout << minimumIncrease(a, b, c); return 0; }
Java
// Java Program to find Minimum // increment in the sides required // to get non-negative area of // a triangle import java.util.*; class GFG { static int minimumIncrease(int a, int b, int c) { // push the three sides // to a array int arr[] = { a, b, c }; // sort the array Arrays.sort(arr); // check if sum is greater // than third side if (arr[0] + arr[1] >= arr[2]) return 0; else return arr[2] - (arr[0] + arr[1]); } // Driver Code public static void main (String[] args) { int a = 3, b = 5, c = 10; System.out.println(minimumIncrease(a, b, c)); } } // This code is contributed // by Shashank
Python 3
# Python program to find Minimum # increase in sides to get # non-negative area of a triangle # Function to return the # minimum increase in side # lengths of the triangle def minimumIncrease(a, b, c) : # push the three sides # to a array arr = [ a, b, c ] # sort the array arr.sort() # check if sum is greater # than third side if arr[0] + arr[1] >= arr[2] : return 0 else : return arr[2] - (arr[0] + arr[1]) # Driver code if __name__ == "__main__" : a, b, c = 3, 5, 10 print(minimumIncrease(a, b, c)) # This code is contributed # by ANKITRAI1
C#
// C# Program to find Minimum // increment in the sides required // to get non-negative area of // a triangle using System; class GFG { static int minimumIncrease(int a, int b, int c) { // push the three sides // to a array int[] arr = { a, b, c }; // sort the array Array.Sort(arr); // check if sum is greater // than third side if (arr[0] + arr[1] >= arr[2]) return 0; else return arr[2] - (arr[0] + arr[1]); } // Driver Code public static void Main () { int a = 3, b = 5, c = 10; Console.Write(minimumIncrease(a, b, c)); } } // This code is contributed // by ChitraNayal
PHP
<?php // PHP program to find Minimum // increase in sides to get // non-negative area of a triangle // Function to return the // minimum increase in side // lengths of the triangle function minimumIncrease($a, $b, $c) { // push the three sides to a array $arr = array($a, $b, $c ); // sort the array sort($arr); // check if sum is greater // than third side if ($arr[0] + $arr[1] >= $arr[2]) return 0; else return $arr[2] - ($arr[0] + $arr[1]); } // Driver Code $a = 3; $b = 5; $c = 10; echo minimumIncrease($a, $b, $c); // This code is contributed // by ChitraNayal ?>
Javascript
<script> // Javascript Program to find Minimum // increment in the sides required // to get non-negative area of // a triangle function minimumIncrease(a , b , c) { // push the three sides // to a array var arr = [ a, b, c ]; // sort the array arr.sort((a,b)=>a-b); // check if sum is greater // than third side if (arr[0] + arr[1] >= arr[2]) return 0; else return arr[2] - (arr[0] + arr[1]); } // Driver Code var a = 3, b = 5, c = 10; document.write(minimumIncrease(a, b, c)); // This code contributed by aashish1995 </script>
Producción:
2
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)