The Wayback Machine - https://web.archive.org/web/20240823160312/https://www.geeksforgeeks.org/polygon-with-maximum-sides-that-can-be-inscribed-in-an-n-sided-regular-polygon/
Open In App

Polygon with maximum sides that can be inscribed in an N-sided regular polygon

Last Updated : 24 Mar, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Easy
10.82%
874

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 = 8
Output: 4
Explanation:
At most a 4 sided polygon can be inscribed inside the given 8-sided polygon as shown below:

Input: N = 3
Output: -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:

  1. 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.
  2. 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;
}


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


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


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


Javascript




<script>
// Javascript program for the
// above approach
 
    // Function to find the
    // maximum sided polygon
    // that can be inscribed
    function MaximumSides( 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
     // Given N
    let N = 8;
 
    // Function Call
    document.write(MaximumSides(N));
 
// This code is contributed by shikhasingrajput
</script>


Output:

4

Time Complexity: O(1)
Auxiliary Space: O(1)



Similar Reads

Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
Given N-sided polygon we need to find the total number of triangles formed by joining the vertices of the given polygon with exactly two sides being common and no side being common.Examples: Input : N = 6 Output : 6 2 The image below is of a triangle forming inside a Hexagon by joining vertices as shown above. The triangle formed has two sides (AB
5 min read
Ratio of area of two nested polygons formed by connecting midpoints of sides of a regular N-sided polygon
Given an N-sided polygon, the task is to find the ratio of the area of the Nth to (N + 1)th N-sided regular nested polygons generated by joining the midpoints of the sides of the original polygon. Examples : Input: N = 3Output: 4.000000Explanation: Ratio of the length of the sides formed by joining the mid-points of the triangle with the length of
4 min read
Program to find Area of Triangle inscribed in N-sided Regular Polygon
Given the triangle inscribed in an N-sided regular polygon with given side length, formed using any 3 vertices of the polygon, the task is to find the area of this triangle. Examples: Input: N = 6, side = 10 Output: 129.904 Input: N = 8, side = 5 Output: 45.2665 Approach: Consider the 1st example: Given is a 6 sided regular polygon ABCDEF with a tr
6 min read
Side of a regular n-sided polygon circumscribed in a circle
Given two integers r and n where n is the number of sides of a regular polygon and r is the radius of the circle this polygon is circumscribed in. The task is to find the length of the side of polygon. Examples: Input: n = 5, r = 11 Output: 12.9256Input: n = 3, r = 5 Output: 8.6576 Approach: Consider the image above and let angle AOB be theta then
4 min read
Apothem of a n-sided regular polygon
Given here the side length a of a regular n-sided polygon, the task is to find the length of its Apothem. Apothem is the line drawn from the center of the polygon that is perpendicular to one of its sides. Examples: Input a = 9, n = 6 Output: 7.79424 Input: a = 8, n = 7 Output: 8.30609 Approach: In the figure, we see the polygon can be divided into
4 min read
Area of largest Circle inscribe in N-sided Regular polygon
Given a regular polygon of N sides with side length a. The task is to find the area of the Circle which inscribed in the polygon. Note : This problem is mixed version of This and This Examples: Input: N = 6, a = 4 Output: 37.6801 Explanation: In this, the polygon have 6 faces and as we see in fig.1 we clearly see that the angle x is 30 degree so th
5 min read
Determine the position of the third person on regular N sided polygon
Given 'N' which represent the regular N sided polygon. Two children are standing on the vertex 'A' and 'B' of this Regular N sided polygon. The task is to determine the number of that vertex another person should stand on so that the sum of the minimum jumps required to reach A and minimum jumps required to reach B is minimized. Note: The vertices
6 min read
Angle between 3 given vertices in a n-sided regular polygon
Given a n-sided regular polygon and three vertices a1, a2 and a3, the task is to find the angle suspended at vertex a1 by vertex a2 and vertex a3. Examples: Input: n = 6, a1 = 1, a2 = 2, a3 = 4 Output: 90 Input: n = 5, a1 = 1, a2 = 2, a3 = 5 Output: 36 Approach: The angle subtended by an edge on the center of n sided regular polygon is 360/n.The an
6 min read
Number of occurrences of a given angle formed using 3 vertices of a n-sided regular polygon
Given an n-sided regular polygon and an angle ?, the task is to find number of occurrences of angle ( Ai, Aj, Ak ) = ? ( i &lt; j &lt; k) in a regular n-gon (regular polygon with n vertices) with vertices marked as A1, A2, ..., An.Examples: Input: n = 4, ang = 90 Output: 4 Input: n = 6, ang = 50 Output: 0 Approach: First we check whether such an an
6 min read
Central angle of a N sided Regular Polygon
Given an integer N representing N sided regular polygon, the task is to find the angle made by the sides on the centre of the polygon that is the central angle. The central angle is the angle formed by the two vertices forming an edge and the centre. Examples: Input: N = 6 Output: 60 Explanation: The polygon is a hexagon with an angle 60 degree. In
3 min read
Find the angle of Rotational Symmetry of an N-sided regular polygon
Given an integer N which is the number of sides of a regular polygon. The task is to find the smallest angle of rotation such that the generated regular polygons have a similar position and dimensions, i.e. the new rotated polygon is in symmetry with the initial one. A shape is said to have a rotation symmetry if there exists a rotation in the rang
4 min read
Find three vertices in an N-sided regular polygon such that the angle subtended at a vertex by two other vertices is closest to A
Given an N-sided regular convex polygon and an angle A in degrees, the task is to find any 3 vertices i, j, and k, such that the angle subtended at vertex j by vertex i and k is closest to A. Note: Each vertex is numbered from 1 to N in an anticlockwise direction starting from any vertex. Examples: Input: N = 3, A = 15Output: 2 1 3Explanation:The g
7 min read
Minimum side of square embedded in Regular polygon with N sides
Given an even number N which represents the number of sides of a regular polygon with N vertices, the task is to find the square of the minimum size such that given Polygon can completely embed in the square. A Polygon is a convex figure and has equal sides and equal angles. All sides have length 1. Embedding: Place Polygon in the square in such wa
8 min read
Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone
Given here is a right circular cone of radius r and perpendicular height h, which is inscribed in a cube which in turn is inscribed in a sphere, the task is to find the radius of the sphere.Examples: Input: h = 5, r = 6 Output: 1.57306 Input: h = 8, r = 11 Output: 2.64156 Approach: Let the side of the cube = aLet the radius of the sphere = RWe know
4 min read
Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle
Given here is a semicircle of radius r, which inscribes a rectangle which in turn inscribes an ellipse. The task is to find the area of this largest ellipse.Examples: Input: r = 5 Output: 19.625 Input: r = 11 Output: 94.985 Approach: Let the, length of the rectangle = l and breadth of the rectangle = bLet, the length of the major axis of the ellips
4 min read
Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
Given a convex polygon with n+2 sides. The task is to calculate the number of ways in which triangles can be formed by connecting vertices with non-crossing line segments.Examples: Input: n = 1 Output: 1 It is already a triangle so it can only be formed in 1 way.Input: n = 2 Output: 2 It can be cut into 2 triangles by using either pair of opposite
5 min read
Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
Given three integers N, X and Y. The task is to find the number of ways to arrange 2*N persons along two sides of a table with N number of chairs on each side such that X persons are on one side and Y persons are on the opposite side. Note: Both X and Y are less than or equals to N.Examples: Input : N = 5, X = 4, Y = 2 Output : 57600 Explanation :
9 min read
Count right angled triangles in a matrix having two of its sides parallel to sides of the matrix
Given a binary matrix arr[][] of dimensions N * M , the task is to count the number of right-angled triangles that can be formed by joining the cells containing the value 1 such that the triangles must have any two of its sides parallel to the sides of the rectangle. Examples: Input: arr[][] = {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}Output: 4Explanation:
8 min read
Area of a triangle inscribed in a rectangle which is inscribed in an ellipse
Given here is an ellipse with axes length 2a and 2b, which inscribes a rectangle of length l and breadth h, which in turn inscribes a triangle.The task is to find the area of this triangle.Examples: Input: a = 4, b = 3 Output: 12 Input: a = 5, b = 2 Output: 10 Approach: We know the Area of the rectangle inscribed within the ellipse is, Ar = 2ab(Ple
4 min read
Area of a square inscribed in a circle which is inscribed in an equilateral triangle
Given here is an equilateral triangle with side length a, which inscribes a circle, which in turn inscribes a square. The task is to find the area of this square.Examples: Input: a = 6 Output: 1 Input: a = 10 Output: 0.527046 Approach: let r be the radius of circle, hence it is the inradius of equilateral triangle, so r = a /(2 * ?3) diagonal of sq
4 min read
Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
Give a rectangle with length l &amp; breadth b, which inscribes a rhombus, which in turn inscribes a circle. The task is to find the radius of this circle.Examples: Input: l = 5, b = 3 Output: 1.28624 Input: l = 6, b = 4 Output: 1.6641 Approach: From the figure, it is clear that diagonals, x &amp; y, are equal to the length and breadth of the recta
4 min read
Number of sides of Polygon when maximum possible number of diagonals is given
Given a number K which is the maximum possible number of diagonals in any polygon, the task is to find the number of sides of that polygon. Examples: Input: 2Output: 4Explanation: A quadrilateral has 2 diagonals at max. Input: 6Output: -1Explanation: No polygon has at most 6 diagonals. Approach: This problem can be solved based on the following mat
6 min read
Probability that the pieces of a broken stick form a n sided polygon
We have a stick of length L. The stick got broken at (n-1) randomly chosen points (lengths of parts can be non-integer or floating point numbers also) so we get n parts. We need to find the probability that these n pieces can form a n sided polygon. Examples: Input : L = 5 n = 3 Output : 0.25 We need to cut rope of length 5 into three parts. First
4 min read
Number of cycles formed by joining vertices of n sided polygon at the center
Given an N-sided regular polygon, we have connected all the vertices at the center of the polygon, thus dividing the polygon into N equal parts. Our task is to the Count of the total number of cycles in the polygon.Note: A Cycle is a closed-loop starting and ending on same point.Examples: Input: N = 3 Output: 7 Explanation: When a 3 sided polygon i
5 min read
Check if an N-sided Polygon is possible from N given angles
Given an array arr[] of N elements, where each element represents an angle(in degrees) of a polygon, the task is to check whether it is possible to make an N-sided polygon with all the given angles or not. If it is possible then print Yes else print No. Examples: Input: N = 3, arr[] = {60, 60, 60}Output: YesExplanation: There exists a triangle(i.e.
4 min read
Find the vertex diagonally opposite to the vertex M from an N-sided polygon
Given two integers N and M, the task is to find the vertex diagonally opposite to the Mth vertex of an N-sided polygon. Examples: Input: N = 6, M = 2 Output: 5 Explanation: It can be observed from the image above that the vertex opposite to vertex 5 is 2. Input: N = 8, M = 5 Output: 1 Explanation: It can be observed from the image above that the ve
3 min read
Find the length of Kth N-sided polygon formed by given operations
Given an integer L, representing the side length of an N-sided regular polygon and integer K, the task is to find the side length of the Kth N-sided regular polygon formed inside the (K - 1)th regular polygon by connecting midpoints of the sides of the (K - 1)th polygon. Examples: Input: N = 3, L = 6, K = 2Output: 3 Input: N = 5, L = 21, K = 7Outpu
5 min read
Find number of diagonals in n sided convex polygon
Given n &gt; 3, find number of diagonals in n sided convex polygon.According to Wikipedia, In geometry, a diagonal is a line segment joining two vertices of a polygon or polyhedron, when those vertices are not on the same edge. Informally, any sloping line is called diagonal. Examples :  Input : 5Output : 5Explanation: Five possible diagonals are :
3 min read
Area of a circle inscribed in a regular hexagon
Given a regular Hexagon with side length a, the task is to find the area of the circle inscribed in it, given that, the circle is tangent to each of the six sides.Examples: Input: a = 4 Output: 37.68 Input: a = 10 Output: 235.5 Approach: From the figure, it is clear that, we can divide the regular hexagon into 6 identical equilateral triangles. We
4 min read
Check if it is possible to create a polygon with given n sides
Given an array arr[] that contains the lengths of n sides that may or may not form a polygon. The task is to determine whether it is possible to form a polygon with all the given sides. Print Yes if possible else print No.Examples: Input: arr[] = {2, 3, 4} Output: YesInput: arr[] = {3, 4, 9, 2} Output: No Approach: In order to create a polygon with
5 min read