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.

**Examples:**

**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**,**X**equilateral triangles of unit length are possible.^{2} - 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 S_{1 }, S_{2 }, S_{3 }, S_{4 }, S_{5 }, S_{6} as:

Number of triangles that can be formed

= ( S_{1}+ S_{2}+ S_{3 })^{2}– S_{1}^{2}– S_{3}^{2}– S_{5}^{2}

Below is the implementation of the above approach:

## C++

`// C++ program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate the ` `// the number of Triangles possible ` `int` `calculateTriangles(` `int` `sides[]) ` `{ ` ` ` `double` `count = ` `pow` `(sides[0] + sides[1] + ` ` ` `sides[2], 2); ` ` ` `count -= ` `pow` `(sides[0], 2); ` ` ` `count -= ` `pow` `(sides[2], 2); ` ` ` `count -= ` `pow` `(sides[4], 2); ` ` ` ` ` `return` `(` `int` `)(count); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// Regular Hexagon ` ` ` `int` `sides[] = { 1, 1, 1, 1, 1, 1 }; ` ` ` `cout << (calculateTriangles(sides)) << endl; ` ` ` ` ` `// Irregular Hexagon ` ` ` `int` `sides1[] = { 2, 2, 1, 3, 1, 2 }; ` ` ` `cout << (calculateTriangles(sides1)) << endl; ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to calculate the ` `// the number of Triangles possible ` `static` `int` `calculateTriangles(` `int` `sides[]) ` `{ ` ` ` `double` `count = Math.pow(sides[` `0` `] + sides[` `1` `] + ` ` ` `sides[` `2` `], ` `2` `); ` ` ` `count -= Math.pow(sides[` `0` `], ` `2` `); ` ` ` `count -= Math.pow(sides[` `2` `], ` `2` `); ` ` ` `count -= Math.pow(sides[` `4` `], ` `2` `); ` ` ` ` ` `return` `(` `int` `)(count); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Regular Hexagon ` ` ` `int` `sides[] = { ` `1` `, ` `1` `, ` `1` `, ` `1` `, ` `1` `, ` `1` `}; ` ` ` `System.out.print((calculateTriangles(sides)) + ` `"\n"` `); ` ` ` ` ` `// Irregular Hexagon ` ` ` `int` `sides1[] = { ` `2` `, ` `2` `, ` `1` `, ` `3` `, ` `1` `, ` `2` `}; ` ` ` `System.out.print((calculateTriangles(sides1)) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to implement ` `# the above approach ` ` ` `# Function to calculate the ` `# the number of Triangles possible ` `def` `calculateTriangles(sides): ` ` ` `count ` `=` `pow` `( sides[` `0` `] ` `+` `sides[` `1` `] ` `+` `sides[` `2` `], ` `2` `) ` ` ` `count ` `-` `=` `pow` `( sides[` `0` `], ` `2` `) ` ` ` `count ` `-` `=` `pow` `( sides[` `2` `], ` `2` `) ` ` ` `count ` `-` `=` `pow` `( sides[` `4` `], ` `2` `) ` ` ` ` ` `return` `int` `(count) ` ` ` `# Driver Code ` ` ` `# Regular Hexagon ` `sides ` `=` `[` `1` `, ` `1` `, ` `1` `, ` `1` `, ` `1` `, ` `1` `] ` `print` `(calculateTriangles(sides)) ` ` ` `# Irregular Hexagon ` `sides ` `=` `[` `2` `, ` `2` `, ` `1` `, ` `3` `, ` `1` `, ` `2` `] ` `print` `(calculateTriangles(sides)) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to calculate the ` `// the number of Triangles possible ` `static` `int` `calculateTriangles(` `int` `[]sides) ` `{ ` ` ` `double` `count = Math.Pow(sides[0] + sides[1] + ` ` ` `sides[2], 2); ` ` ` `count -= Math.Pow(sides[0], 2); ` ` ` `count -= Math.Pow(sides[2], 2); ` ` ` `count -= Math.Pow(sides[4], 2); ` ` ` ` ` `return` `(` `int` `)(count); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Regular Hexagon ` ` ` `int` `[]sides = { 1, 1, 1, 1, 1, 1 }; ` ` ` `Console.Write((calculateTriangles(sides)) + ` `"\n"` `); ` ` ` ` ` `// Irregular Hexagon ` ` ` `int` `[]sides1 = { 2, 2, 1, 3, 1, 2 }; ` ` ` `Console.Write((calculateTriangles(sides1)) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

6 19

**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:

- Maximum count of Equilateral Triangles that can be formed within given Equilateral Triangle
- Largest hexagon that can be inscribed within an equilateral triangle
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Ratio of area of one circle to the equilateral triangle when three equal circles are placed inside an equilateral triangle
- Equilateral Triangles using Matchsticks
- Puzzle | Form Three Equilateral Triangles
- Count number of triangles possible with length of sides not exceeding N
- Area of hexagon with given diagonal length
- Count number of right triangles possible with a given perimeter
- Count number of triangles possible for the given sides range
- Find length of Diagonal of Hexagon
- Minimize number of cuts required to break N length stick into N unit length sticks
- Count the number of possible triangles
- Count of 0s in an N-level hexagon
- Number of triangles possible with given lengths of sticks which are powers of 2
- Count of distinct rectangles inscribed in an equilateral triangle
- Number of possible Triangles in a Cartesian coordinate system
- Find all possible triangles with XOR of sides zero
- Program to find all possible triangles having same Area and Perimeter
- Area of 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.