Given two positive integers N and M which denotes the sides of the convex regular polygon where N < M, the task is to check whether polygons have the same center or not if N-sided polygon was inscribed in an M-sided polygon.
Center of Polygon: Point inside a polygon which is equidistant from each vertex of the polygon.
Examples:
Input: N = 9, M = 3
Output: YES
Explanation:
Polygon of side 3 when inscribed in a polygon of side 9, then both polygons have same center.Input: N = 10, M = 3
Output: NO
Explanation:
Polygon of side 3 when inscribed in a polygon of side 10, then both polygons don’t have same center.
Approach: The key observation in this problem is that when M % N == 0, that means the sides of N-sided polygon equally covers the sides of M-sided polygon, which means both the polygons have same center.
Algorithm:
- Check if M is divisible by N, If yes then both the polygons have same center.
- Otherwise both polygons have the different centers.
Below is the implementation of the above approach:
// C++ implementation to check whether // two convex polygons have same center #include<bits/stdc++.h> using namespace std;
// Function to check whether two convex // polygons have the same center or not int check( int n, int m){
if (m % n == 0){
cout << "YES" ;
}
else {
cout << "NO" ;
}
return 0;
} // Driver Code int main()
{ int n = 5;
int m = 10;
check(n, m);
return 0;
} |
// Java implementation to check whether // two convex polygons have same center class GFG{
// Function to check whether two convex // polygons have the same center or not static int check( int n, int m){
if (m % n == 0 ){
System.out.print( "YES" );
}
else {
System.out.print( "NO" );
}
return 0 ;
} // Driver Code public static void main(String[] args)
{ int n = 5 ;
int m = 10 ;
check(n, m);
} } // This code is contributed by sapnasingh4991 |
# Python3 implementation to check whether # two convex polygons have same center # Function to check whether two convex # polygons have the same center or not def check(n, m):
if (m % n = = 0 ):
print ( "YES" )
else :
print ( "NO" )
# Driver Code n = 5
m = 10
check(n, m) # This code is contributed by mohit kumar 29 |
// C# implementation to check whether // two convex polygons have same center using System;
class GFG{
// Function to check whether two convex // polygons have the same center or not static int check( int n, int m){
if (m % n == 0){
Console.Write( "YES" );
}
else {
Console.Write( "NO" );
}
return 0;
} // Driver Code public static void Main(String[] args)
{ int n = 5;
int m = 10;
check(n, m);
} } // This code is contributed by Rajput-Ji |
YES
Performance Analysis:
- Time Complexity: O(1).
- Auxiliary Space: O(1).
Recommended Posts:
- Check if given polygon is a convex polygon or not
- Dynamic Convex hull | Adding Points to an Existing Convex Hull
- Polygon with maximum sides that can be inscribed in an N-sided regular polygon
- Check if the given point lies inside given N points of a Convex Polygon
- Distance of chord from center when distance between center and another equal length chord is given
- Find number of diagonals in n sided convex polygon
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Number of cycles formed by joining vertices of n sided polygon at the center
- Check whether two strings contain same characters in same order
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Regular polygon using only 1s in a binary numbered circle
- Program to find the Interior and Exterior Angle of a Regular Polygon
- Program to find the Circumcircle of any regular polygon
- Program to find the Perimeter of a Regular Polygon
- Program to find Area of Triangle inscribed in N-sided Regular Polygon
- Side of a regular n-sided polygon circumscribed in a circle
- Area of a n-sided regular polygon with given side length
- Area of a n-sided regular polygon with given Radius
- Length of Diagonal of a n-sided regular polygon
- Apothem of 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.