Given a regular polygon of **N **sides, the task is to find the maximum sided polygon that can be inscribed inside the given polygon by joining non-adjacent vertices. Print** -1**, if no such polygon exist.

**Examples:**

Input:N = 8Output:4Explanation:At most a 4 sided polygon can be inscribed inside the given 8-sided polygon as shown below:

Input:N = 3Output:-1

**Approach:** The idea is to observe the fact that a regular polygon can be inscribed inside another regular polygon of **N** sides if **N** is even. Follow the below steps to solve the problem:

- If
**N**is even, then the inscribed polygon with the maximum sides can be formed by joining the non-adjacent vertices. Therefore, print**N/2**as the required answer. - Otherwise, print
**-1**as no regular polygon can be inscribed inside an odd-sided polygon.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the maximum` `// sided polygon that can be inscribed` `int` `MaximumSides(` `int` `n)` `{` ` ` `// Base Case` ` ` `if` `(n < 4)` ` ` `return` `-1;` ` ` `// Return n/2 if n is even` ` ` `// Otherwise, return -1` ` ` `return` `n % 2 == 0 ? n / 2 : -1;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given N` ` ` `int` `N = 8;` ` ` `// Function Call` ` ` `cout << MaximumSides(N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the ` `// above approach` `import` `java.util.*;` `class` `GFG{` `// Function to find the ` `// maximum sided polygon ` `// that can be inscribed` `static` `int` `MaximumSides(` `int` `n)` `{` ` ` `// Base Case` ` ` `if` `(n < ` `4` `)` ` ` `return` `-` `1` `;` ` ` `// Return n/2 if n is ` ` ` `// even Otherwise, return -1` ` ` `return` `n % ` `2` `== ` `0` `?` ` ` `n / ` `2` `: -` `1` `;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Given N` ` ` `int` `N = ` `8` `;` ` ` `// Function Call` ` ` `System.out.print(MaximumSides(N));` `}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function to find the maximum sided ` `# polygon that can be inscribed` `def` `MaximumSides(n):` ` ` ` ` `# Base Case` ` ` `if` `(n < ` `4` `):` ` ` `return` `-` `1` ` ` `# Return n/2 if n is even` ` ` `# Otherwise, return -1` ` ` `if` `n ` `%` `2` `=` `=` `0` `:` ` ` `return` `n ` `/` `/` `2` ` ` ` ` `return` `-` `1` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given N` ` ` `N ` `=` `8` ` ` `# Function Call` ` ` `print` `(MaximumSides(N))` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program for the ` `// above approach` `using` `System;` `class` `GFG{` `// Function to find the ` `// maximum sided polygon ` `// that can be inscribed` `static` `int` `MaximumSides(` `int` `n)` `{` ` ` `// Base Case` ` ` `if` `(n < 4)` ` ` `return` `-1;` ` ` `// Return n/2 if n is ` ` ` `// even Otherwise, return -1` ` ` `return` `n % 2 == 0 ?` ` ` `n / 2 : -1;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `// Given N` ` ` `int` `N = 8;` ` ` `// Function Call` ` ` `Console.Write(MaximumSides(N));` `}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(1)**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 two common sides and no common sides
- Minimum side of square embedded in Regular polygon with N sides
- Program to find Area of Triangle inscribed in N-sided Regular Polygon
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube
- Largest sphere that can be inscribed in a right circular cylinder inscribed in a frustum
- Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone
- Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Count right angled triangles in a matrix having two of its sides parallel to sides of the matrix
- Area of a triangle inscribed in a rectangle which is inscribed in an ellipse
- Area of a square inscribed in a circle which is inscribed in an equilateral triangle
- Area of a circle inscribed in a rectangle which is inscribed in a semicircle
- Area of a square inscribed in a circle which is inscribed in a hexagon
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon

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.