Check if it is possible to create a polygon with given n sides
Given an array arr[] that contain 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: YesInput: 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 then 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"; ?> |
Yes
Recommended Posts:
- Check if it is possible to create a polygon with a given angle
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Check whether triangle is valid or not if sides are given
- Check if given four integers (or sides) make rectangle
- How to check if a given point lies inside or outside a polygon?
- Check whether right angled triangle is valid or not for large sides
- Find all possible triangles with XOR of sides zero
- Find other two sides of a right angle triangle
- Area of the circumcircle of any triangles with sides given
- Maximize volume of cuboid with given sum of sides
- Minimum Cost Polygon Triangulation
- Area of a polygon with given n ordered vertices
- Apothem of a n-sided regular polygon
- Minimum area of a Polygon with three points given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



