Sunday, October 6, 2024
Google search engine
HomeData Modelling & AICheck if an N-sided Polygon is possible from N given angles

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




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


Output: 

Yes

 

Time Complexity: O(N), where N is the length of the array. 
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments