给定一个数组arr [] ,该数组包含可能会或可能不会形成多边形的n个边的长度。任务是确定是否有可能在所有给定的边上形成多边形。打印是如果可能的话其他打印无。
例子:
Input: arr[] = {2, 3, 4}
Output: Yes
Input: arr[] = {3, 4, 9, 2}
Output: No
方法:为了创建具有给定n个边的多边形,多边形的边必须满足一定的属性。
Property: The length of the every given side must be less than the sum of the other remaining sides.
在给定的边中找到最大的边。然后,检查它是否小于其他边的总和。如果较小,则打印“是”,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
$maxS)
return true;
return false;
}
// Driver code
$a = array(2, 3, 4);
$n = count($a);
if(isPossible($a, $n))
echo "Yes";
else
echo "No";
?>
输出:
Yes