Check if it is possible to create a polygon with given n sides
Last Updated :
17 Feb, 2023
Given an array arr[] that contains the lengths of n sides that may or may not form a polygon. The task is to determine whether it is possible to form a polygon with all the given sides. Print Yes if possible else print No.
Examples:
Input: arr[] = {2, 3, 4}
Output: Yes
Input: arr[] = {3, 4, 9, 2}
Output: No
Approach: In order to create a polygon with given n sides, there is a certain property that must be satisfied by the sides of the polygon.
Property: The length of the every given side must be less than the sum of the other remaining sides.
Find the largest side among the given sides. Then, check whether it is smaller than the sum of the other sides or not. If it is smaller than print Yes else print No.
Below is the implementation of the above approach:
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>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Check if it is possible to create a polygon with a given angle Given an angle a where, 1\le a< 180 . The task is to check whether it is possible to make a regular polygon with all of its interior angle equal to a . If possible then print "YES", otherwise print "NO" (without quotes). Examples: Input: angle = 90 Output: YES Polygons with sides 4 is possible wi
4 min read
Check if an N-sided Polygon is possible from N given angles Given an array arr[] of N elements, where each element represents an angle(in degrees) of a polygon, the task is to check whether it is possible to make an N-sided polygon with all the given angles or not. If it is possible then print Yes else print No. Examples: Input: N = 3, arr[] = {60, 60, 60}Ou
4 min read
Check if given polygon is a convex polygon or not Given a 2D array point[][] with each row of the form {X, Y}, representing the co-ordinates of a polygon in either clockwise or counterclockwise sequence, the task is to check if the polygon is a convex polygon or not. If found to be true, then print "Yes" . Otherwise, print "No".In a convex polygon,
9 min read
Polygon with maximum sides that can be inscribed in an N-sided regular polygon Given a regular polygon of N sides, the task is to find the maximum sided polygon that can be inscribed inside the given polygon by joining non-adjacent vertices. Print -1, if no such polygon exist. Examples: Input: N = 8Output: 4Explanation:At most a 4 sided polygon can be inscribed inside the give
3 min read
Sum of sides of largest and smallest child polygons possible from a given polygon Given an integer A representing the external angle (in degrees) of a regular convex polygon, the task is to find the sum of the sides of the largest and smallest secondary polygons formed such that each edge of the secondary polygon is a chord of the primary polygon. If it is not possible to form su
4 min read