Largest Square that can be inscribed within a hexagon
Given a regular hexagonof side length a, the task is to find the area of the largest square that can be inscribed within it.
Input: a = 6
Input: a = 8
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.
Approach: The square we will derive will have the same centre and axes of the hexagon. This is because the square will become smaller if we will rotate it.
The sides of the hexagonal are equal i.e. a = b + c.
Now, let d be the length of the side of the inscribed square,
Then the top side of the square, d = 2 * c * sin(60).
And, the left side of the square, d = a + 2 * b * sin(30).
Substituting for c, d = 2 * (a – b) * sin(60).
Now taking d and re-arranging, we get, b / a = (2 * sin(60) – 1) / (2 * (sin(30) + sin(60)))
So, b / a = 2 – √3
Now, substituting the relation of b and a in the left hand side equation of square, we get,
d / a = 3 – √3 i.e. d / a = 1.268
Therefore, d = 1.268 * a
Below is the implementation of the above approach: