Check whether two convex regular polygon have same center or not
Last Updated :
01 Nov, 2023
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++
#include<bits/stdc++.h>
using namespace std;
int check( int n, int m){
if (m % n == 0){
cout << "YES" ;
}
else {
cout << "NO" ;
}
return 0;
}
int main()
{
int n = 5;
int m = 10;
check(n, m);
return 0;
}
|
Java
class GFG{
static int check( int n, int m){
if (m % n == 0 ){
System.out.print( "YES" );
}
else {
System.out.print( "NO" );
}
return 0 ;
}
public static void main(String[] args)
{
int n = 5 ;
int m = 10 ;
check(n, m);
}
}
|
Python3
def check(n, m):
if (m % n = = 0 ):
print ( "YES" )
else :
print ( "NO" )
n = 5
m = 10
check(n, m)
|
C#
using System;
class GFG{
static int check( int n, int m){
if (m % n == 0){
Console.Write( "YES" );
}
else {
Console.Write( "NO" );
}
return 0;
}
public static void Main(String[] args)
{
int n = 5;
int m = 10;
check(n, m);
}
}
|
Javascript
<script>
function check(n, m)
{
if (m % n == 0)
{
document.write( "YES" );
}
else
{
document.write( "NO" );
}
return 0;
}
var n = 5;
var m = 10;
check(n, m);
</script>
|
Performance Analysis:
- Time Complexity: O(1).
- Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...