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 = 6Output:1Explanation:There is only one nested polygon i.e., Triangle whose sides are the chords of the immediate parent polygon i.e., Hexagon.

Input:N = 12Output:2Explanation: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++

`// C++ program for the above approach ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function that counts the nested ` `// polygons inside another polygons ` `int` `countNestedPolygons(` `int` `sides) ` `{ ` ` ` `// Stores the count ` ` ` `int` `count = 0; ` ` ` ` ` `// Child polygons can only existss ` ` ` `// if parent polygon has sides > 5 ` ` ` `while` `(sides > 5) { ` ` ` ` ` `// Get next nested polygon ` ` ` `sides /= 2; ` ` ` `count += 1; ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given side of polygon ` ` ` `int` `N = 12; ` ` ` ` ` `// Function Call ` ` ` `cout << countNestedPolygons(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `// Function that counts the nested ` `// polygons inside another polygons ` `static` `int` `countNestedPolygons(` `int` `sides) ` `{ ` ` ` `// Stores the count ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Child polygons can only existss ` ` ` `// if parent polygon has sides > 5 ` ` ` `while` `(sides > ` `5` `) ` ` ` `{ ` ` ` ` ` `// Get next nested polygon ` ` ` `sides /= ` `2` `; ` ` ` `count += ` `1` `; ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// Given side of polygon ` ` ` `int` `N = ` `12` `; ` ` ` ` ` `// Function Call ` ` ` `System.out.print(countNestedPolygons(N)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function that counts the nested ` `# polygons inside another polygons ` `def` `countNestedPolygons(sides): ` ` ` ` ` `# Stores the count ` ` ` `count ` `=` `0` ` ` ` ` `# Child polygons can only exists ` ` ` `# if parent polygon has sides > 5 ` ` ` `while` `(sides > ` `5` `): ` ` ` ` ` `# Get next nested polygon ` ` ` `sides ` `/` `/` `=` `2` ` ` `count ` `+` `=` `1` ` ` ` ` `# Return the count ` ` ` `return` `count ` ` ` `# Driver Code ` ` ` `# Given side of polygon ` `N ` `=` `12` ` ` `# Function call ` `print` `(countNestedPolygons(N)) ` ` ` `# This code is contributed by vishu2908 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function that counts the nested ` `// polygons inside another polygons ` `static` `int` `countNestedPolygons(` `int` `sides) ` `{ ` ` ` ` ` `// Stores the count ` ` ` `int` `count = 0; ` ` ` ` ` `// Child polygons can only existss ` ` ` `// if parent polygon has sides > 5 ` ` ` `while` `(sides > 5) ` ` ` `{ ` ` ` ` ` `// Get next nested polygon ` ` ` `sides /= 2; ` ` ` `count += 1; ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Given side of polygon ` ` ` `int` `N = 12; ` ` ` ` ` `// Function call ` ` ` `Console.Write(countNestedPolygons(N)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(log N), where N is the number of vertices in the given polygon.**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of triangles formed by joining vertices of n-sided polygon with one side common
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Number of cycles formed by joining vertices of n sided polygon at the center
- Count of squares that can be drawn without lifting the pencil
- Area of largest semicircle that can be drawn inside a square
- Find the remaining vertices of a square from two given vertices
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Tangents between two Convex Polygons
- Count of distinct graphs that can be formed with N vertices
- Length of the perpendicular bisector of the line joining the centers of two circles
- Check if number formed by joining two Numbers is Perfect Cube
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Area of a polygon with given n ordered vertices
- Check if a path exists in a tree with K vertices present or are at most at a distance D
- Minimum initial vertices to traverse whole matrix with given conditions
- Number of Simple Graph with N Vertices and M Edges
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices
- Maximize the sum of products of the degrees between any two vertices of the tree
- Make a tree with n vertices , d diameter and at most vertex degree k
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree

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.