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 |
Yes
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sum of internal angles of a Polygon
- Check if a triangle of positive area is possible with the given angles
- Check whether the triangle is valid or not if angles are given
- Check whether Quadrilateral is valid or not if angles are given
- Check if it is possible to create a polygon with a given angle
- Check if it is possible to create a polygon with given n sides
- Find all angles of a given triangle
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Program to find smallest difference of angles of two parts of a given circle
- Length of remaining two sides of a Triangle from a given side and its adjacent angles
- Check if the given point lies inside given N points of a Convex Polygon
- Find all angles of a triangle in 3D
- Find other two sides and angles of a right angle triangle
- Program to find the angles of a quadrilateral
- How to check if a given point lies inside or outside a polygon?
- Check whether two convex regular polygon have same center or not
- Area of a polygon with given n ordered vertices
- Area of a n-sided regular polygon with given side length
- Area of a n-sided regular polygon with given Radius
- Angle between 3 given vertices in a n-sided regular polygon
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.

