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++
#include <iostream>
using namespace std;
void checkValidPolygon( int arr[], int N)
{
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += arr[i];
}
if (sum == 180 * (N - 2))
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int N = 3;
int arr[] = { 60, 60, 60 };
checkValidPolygon(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void checkValidPolygon( int arr[], int N)
{
int sum = 0 ;
for ( int i = 0 ; i < N; i++)
{
sum += arr[i];
}
if (sum == 180 * (N - 2 ))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String[] args)
{
int N = 3 ;
int arr[] = { 60 , 60 , 60 };
checkValidPolygon(arr, N);
}
}
|
Python3
def checkValidPolygon(arr, N):
Sum = 0
for i in range (N):
Sum + = arr[i]
if Sum = = 180 * (N - 2 ):
print ( "Yes" )
else :
print ( "No" )
N = 3
arr = [ 60 , 60 , 60 ]
checkValidPolygon(arr, N)
|
C#
using System;
class GFG{
static void checkValidPolygon( int []arr, int N)
{
int sum = 0;
for ( int i = 0; i < N; i++)
{
sum += arr[i];
}
if (sum == 180 * (N - 2))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main( string [] args)
{
int N = 3;
int []arr = { 60, 60, 60 };
checkValidPolygon(arr, N);
}
}
|
Javascript
<script>
function checkValidPolygon(arr, N)
{
var sum = 0;
for ( var i = 0; i < N; i++)
{
sum += arr[i];
}
if (sum == 180 * (N - 2))
document.write( "Yes" );
else
document.write( "No" );
}
var N = 3;
var arr = [ 60, 60, 60 ];
checkValidPolygon(arr, N);
</script>
|
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(1)