Dada una array arr[] que contiene las longitudes de n lados que pueden o no formar un polígono. La tarea es determinar si es posible formar un polígono con todos los lados dados. Escriba Sí si es posible, de lo contrario, escriba No.
Ejemplos:
Entrada: arr[] = {2, 3, 4}
Salida: Sí
Entrada: arr[] = {3, 4, 9, 2}
Salida: No
Enfoque: Para crear un polígono con n lados dados, hay una cierta propiedad que deben cumplir los lados del polígono.
Propiedad: La longitud de cada lado dado debe ser menor que la suma de los otros lados restantes.
Encuentra el lado más grande entre los lados dados. Luego, verifica si es más pequeño que la suma de los otros lados o no. Si es más pequeño, escriba Sí , de lo contrario, escriba No.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if it is possible // to form a polygon with the given sides bool isPossible(int a[], int n) { // Sum stores the sum of all the sides // and maxS stores the length of // the largest side int sum = 0, maxS = 0; for (int i = 0; i < n; i++) { sum += a[i]; maxS = max(a[i], maxS); } // If the length of the largest side // is less than the sum of the // other remaining sides if ((sum - maxS) > maxS) return true; return false; } // Driver code int main() { int a[] = { 2, 3, 4 }; int n = sizeof(a) / sizeof(a[0]); if (isPossible(a, n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the approach class GFG { // Function that returns true if it is possible // to form a polygon with the given sides static boolean isPossible(int a[], int n) { // Sum stores the sum of all the sides // and maxS stores the length of // the largest side int sum = 0, maxS = 0; for (int i = 0; i < n; i++) { sum += a[i]; maxS = Math.max(a[i], maxS); } // If the length of the largest side // is less than the sum of the // other remaining sides if ((sum - maxS) > maxS) return true; return false; } // Driver code public static void main(String[] args) { int a[] = { 2, 3, 4 }; int n = a.length; if (isPossible(a, n)) System.out.print("Yes"); else System.out.print("No"); } }
Python
# Python 3 implementation of the approach # Function to check whether # it is possible to create a # polygon with given sides length def isPossible(a, n): # Sum stores the sum of all the sides # and maxS stores the length of # the largest side sum = 0 maxS = 0 for i in range(n): sum += a[i] maxS = max(a[i], maxS) # If the length of the largest side # is less than the sum of the # other remaining sides if ((sum - maxS) > maxS): return True return False # Driver code a =[2, 3, 4] n = len(a) if(isPossible(a, n)): print("Yes") else: print("No")
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if it is possible // to form a polygon with the given sides static bool isPossible(int[] a, int n) { // Sum stores the sum of all the sides // and maxS stores the length of // the largest side int sum = 0, maxS = 0; for (int i = 0; i < n; i++) { sum += a[i]; maxS = Math.Max(a[i], maxS); } // If the length of the largest side // is less than the sum of the // other remaining sides if ((sum - maxS) > maxS) return true; return false; } // Driver code static void Main() { int[] a = { 2, 3, 4 }; int n = a.Length; if (isPossible(a, n)) Console.Write("Yes"); else Console.Write("No"); } }
PHP
<?php // PHP implementation of the approach // Function that returns true if it is possible // to form a polygon with the given sides function isPossible($a, $n) { // Sum stores the sum of all the sides // and maxS stores the length of // the largest side $sum = 0; $maxS = 0; for ($i = 0; $i < $n; $i++) { $sum += $a[$i]; $maxS = max($a[$i], $maxS); } // If the length of the largest side // is less than the sum of the // other remaining sides if (($sum - $maxS) > $maxS) return true; return false; } // Driver code $a = array(2, 3, 4); $n = count($a); if(isPossible($a, $n)) echo "Yes"; else echo "No"; ?>
Javascript
<script> // Javascript implementation of the approach // Function that returns true if it is possible // to form a polygon with the given sides function isPossible( a, n) { // Sum stores the sum of all the sides // and maxS stores the length of // the largest side let sum = 0, maxS = 0; for (let i = 0; i < n; i++) { sum += a[i]; maxS = Math.max(a[i], maxS); } // If the length of the largest side // is less than the sum of the // other remaining sides if ((sum - maxS) > maxS) return true; return false; } // Driver Code let a = [ 2, 3, 4 ]; let n = a.length; if (isPossible(a, n)) document.write("Yes"); else document.write("No"); </script>
Yes
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Tanvi_Garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA