Given a regular polygon with N sides. The task is to find the count of polygons that can be drawn from the given polygon by joining the vertices of the given polygon internally.
Examples:
Input: N = 6
Output: 1
Explanation:

There is only one nested polygon i.e., Triangle whose sides are the chords of the immediate parent polygon i.e., Hexagon.
Input: N = 12
Output: 2
Explanation:

There are two nested polygons. First one is a Hexagon and second one is a Triangle. The sides of both of the nested polygons are actually chords of their immediate parent polygon.
Approach: To solve this problem observe the following:
- Polygons with a number of sides less than or equal to 5 can not produce nested polygons, i.e. polygons of sides ?5 will always have at least one side overlapping with its nested polygon.
- Each side of a nested polygon takes two consecutive sides of the immediate parent polygon.
With the above observations, it is easy to conclude that number of sides of each nested polygon is half of the number of sides of its immediate parent polygon. So, we keep dividing N by 2, and increment the counter for nested polygons, until N becomes less than or equal to 5.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int countNestedPolygons( int sides)
{
int count = 0;
while (sides > 5) {
sides /= 2;
count += 1;
}
return count;
}
int main()
{
int N = 12;
cout << countNestedPolygons(N);
return 0;
}
|
Java
class GFG{
static int countNestedPolygons( int sides)
{
int count = 0 ;
while (sides > 5 )
{
sides /= 2 ;
count += 1 ;
}
return count;
}
public static void main(String[] args)
{
int N = 12 ;
System.out.print(countNestedPolygons(N));
}
}
|
Python3
def countNestedPolygons(sides):
count = 0
while (sides > 5 ):
sides / / = 2
count + = 1
return count
N = 12
print (countNestedPolygons(N))
|
C#
using System;
class GFG{
static int countNestedPolygons( int sides)
{
int count = 0;
while (sides > 5)
{
sides /= 2;
count += 1;
}
return count;
}
public static void Main(String[] args)
{
int N = 12;
Console.Write(countNestedPolygons(N));
}
}
|
Javascript
<script>
function countNestedPolygons(sides)
{
var count = 0;
while (sides > 5)
{
sides /= 2;
count += 1;
}
return count;
}
var N = 12;
document.write(countNestedPolygons(N));
</script>
|
Time Complexity: O(log N), where N is the number of vertices in the given polygon.
Auxiliary Space: O(1)