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


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

Check out this Author's contributed articles.

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.



Improved By : offbeat, rutvik_56