Check if an N-sided Polygon is possible from N given angles
Last Updated :
13 Apr, 2021
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}
Output: Yes
Explanation: There exists a triangle(i.e. a polygon) satisfying the above angles. Hence the output is Yes.
Input: N = 4, arr[] = {90, 90, 90, 100}
Output: No
Explanation: There does not exist any polygon satisfying the above angles. Hence the output is No.
Approach: A N-sided polygon is only possible if the sum of all the given angles is equal to 180*(N-2). Therefore the ides is to find the sum of all the angles given in the array arr[] and if the sum is equal to 180*(N-2) then print Yes, else print No.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <iostream>
using namespace std;
// Function to check if the polygon
// exists or not
void checkValidPolygon(int arr[], int N)
{
// Initialize a variable to
// store the sum of angles
int sum = 0;
// Loop through the array and
// calculate the sum of angles
for (int i = 0; i < N; i++) {
sum += arr[i];
}
// Check the condition for
// an N-side polygon
if (sum == 180 * (N - 2))
cout << "Yes";
else
cout << "No";
}
// Driver Code
int main()
{
int N = 3;
// Given array arr[]
int arr[] = { 60, 60, 60 };
// Function Call
checkValidPolygon(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if the polygon
// exists or not
static void checkValidPolygon(int arr[], int N)
{
// Initialize a variable to
// store the sum of angles
int sum = 0;
// Loop through the array and
// calculate the sum of angles
for(int i = 0; i < N; i++)
{
sum += arr[i];
}
// Check the condition for
// an N-side polygon
if (sum == 180 * (N - 2))
System.out.println("Yes");
else
System.out.println("No");
}
// Driver code
public static void main(String[] args)
{
int N = 3;
// Given array arr[]
int arr[] = { 60, 60, 60 };
// Function call
checkValidPolygon(arr, N);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to check if the polygon
# exists or not
def checkValidPolygon(arr, N):
# Initialize a variable to
# store the sum of angles
Sum = 0
# Loop through the array and
# calculate the sum of angles
for i in range(N):
Sum += arr[i]
# Check the condition for
# an N-side polygon
if Sum == 180 * (N - 2):
print("Yes")
else:
print("No")
# Driver Code
N = 3
# Given array arr[]
arr = [ 60, 60, 60 ]
# Function Call
checkValidPolygon(arr, N)
# This code is contributed by divyeshrabadiya07
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if the polygon
// exists or not
static void checkValidPolygon(int []arr, int N)
{
// Initialize a variable to
// store the sum of angles
int sum = 0;
// Loop through the array and
// calculate the sum of angles
for(int i = 0; i < N; i++)
{
sum += arr[i];
}
// Check the condition for
// an N-side polygon
if (sum == 180 * (N - 2))
Console.Write("Yes");
else
Console.Write("No");
}
// Driver code
public static void Main(string[] args)
{
int N = 3;
// Given array arr[]
int []arr = { 60, 60, 60 };
// Function call
checkValidPolygon(arr, N);
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// Javascript program for the above approach
// Function to check if the polygon
// exists or not
function checkValidPolygon(arr, N)
{
// Initialize a variable to
// store the sum of angles
var sum = 0;
// Loop through the array and
// calculate the sum of angles
for(var i = 0; i < N; i++)
{
sum += arr[i];
}
// Check the condition for
// an N-side polygon
if (sum == 180 * (N - 2))
document.write("Yes");
else
document.write("No");
}
// Driver code
var N = 3;
// Given array arr[]
var arr = [ 60, 60, 60 ];
// Function call
checkValidPolygon(arr, N);
// This code is contributed by Kirti
</script>
Time Complexity: O(N), where N is the length of the array.
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 it is possible to create a polygon with given n sides 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: YesInput: arr[] = {3, 4, 9, 2} Outp
5 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
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
Angle between 3 given vertices in a n-sided regular polygon Given a n-sided regular polygon and three vertices a1, a2 and a3, the task is to find the angle suspended at vertex a1 by vertex a2 and vertex a3. Examples: Input: n = 6, a1 = 1, a2 = 2, a3 = 4 Output: 90 Input: n = 5, a1 = 1, a2 = 2, a3 = 5 Output: 36 Approach: The angle subtended by an edge on the
6 min read