Given an array S consisting of the lengths of the 6 sides of a Hexagon, the task is to calculate the number of equilateral triangles of unit length that can be made from the given hexagon.
Approach:The following observations need to be made to solve the given problem:
- Consider an equilateral triangle of ‘X’ side length. It has been divided into smaller triangles of unit length each, by drawing lines parallel to its sides.
- Below are the images of three such equilateral triangles:
- In each of the above three examples, the count of unit length equilateral triangles possible are:
- X = 2: 4 equilateral triangles of 1 unit length side.
- X = 3: 9 equilateral triangles of 1 unit length side.
- X = 5: 25 equilateral triangles of 1 unit length side.
- By observation, it is clear that, for an equilateral triangle of side length X, X2 equilateral triangles of unit length are possible.
- Extending this observation to Hexagons, inscribe Hexagons inside the equilateral triangles, as shown below:
- It can be observed that by removing a certain number of mini triangles from the bigger triangle, the hexagon with given dimensions can be found.
The formula for counting the number of triangles of unit length can be generalized for a Hexagon having six sides S1 , S2 , S3 , S4 , S5 , S6 as:
Number of triangles that can be formed = ( S1 + S2 + S3 )2 – S12 – S32 – S52
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.