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}
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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; 
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

I am persuing Btech in CSE from Madan Mohan Malaviya University of Technology, Gorakhpur 6 star on codechef(handle - gjaiswal108) with rating 2215(max) I love competitive programming and participate in various online contests My other interests are in web scraping and web development

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.