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.
Input: N = 3, L = 6, K = 2
Input: N = 5, L = 21, K = 7
Approach: The given problem can be solved based on the following observations:
- Suppose, \theta represents the interior angle of the N-sided polygon which is the same for all the polygons formed inside i.e.,
- The length of the side of the first polygon formed inside by connecting midpoints of the sides can be calculated using the formula as .
- The length of the side of the Kth polygon formed inside the (K – 1)th polygon and connecting midpoints of the sides of the (K – 1)th polygon is
Follow the steps below to solve the problem:
- Find the interior angle of the N-sided regular polygon and store it in a variable say angle in radians.
- Print the side length after calculating the side length of Kth N sided regular polygon by the above-discussed formula .
Below is the implementation of the above approach:
Time Complexity: O(log K)
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live