# Number of triangles possible with given lengths of sticks which are powers of 2

Given an array of **N** integers where **arr[i]** denotes the number of sticks of length **2 ^{i}**. The task is to find the number of triangles possible with given lengths having area

**≥ 0**.

**Note:**Every stick can only be used once.

**Examples:**

Input:a[] = {1, 2, 2, 2, 2}

Output:3

All possible triangles are:

(2^{0}, 2^{4}, 2^{4}), (2^{1}, 2^{3}, 2^{3}), (2^{1}, 2^{2}, 2^{2}).

Input:a[] = {3, 3, 3}

Output:3

**Approach:** The main observation is that the triangles with area **≥ 0** can only be formed if there are three same lengths of sticks or one different and two similar lengths of sticks. Hence greedily iterate from the back and count the number of pairs of same length sticks available which is **arr[i] / 2**. But if there is a stick remaining, then a pair and a stick is used to form a triangle. In the end, the total number of sticks left is calculated which is **2 * pairs** and the number of triangles that can be formed with these remaining sticks will be **(2 * pairs) / 3**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// number of positive area triangles ` `int` `countTriangles(` `int` `a[], ` `int` `n) ` `{ ` ` ` ` ` `// To store the count of ` ` ` `// total triangles ` ` ` `int` `cnt = 0; ` ` ` ` ` `// To store the count of pairs of sticks ` ` ` `// with equal lengths ` ` ` `int` `pairs = 0; ` ` ` ` ` `// Back-traverse and count ` ` ` `// the number of triangles ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) { ` ` ` ` ` `// Count the number of pairs ` ` ` `pairs += a[i] / 2; ` ` ` ` ` `// If we have one remaining stick ` ` ` `// and we have a pair ` ` ` `if` `(a[i] % 2 == 1 && pairs > 0) { ` ` ` ` ` `// Count 1 triangle ` ` ` `cnt += 1; ` ` ` ` ` `// Reduce one pair ` ` ` `pairs -= 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Count the remaining triangles ` ` ` `// that can be formed ` ` ` `cnt += (2 * pairs) / 3; ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 2, 2, 2, 2 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << countTriangles(a, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the ` `# number of positive area triangles ` `def` `countTriangles(a, n): ` ` ` ` ` `# To store the count of ` ` ` `# total triangles ` ` ` `cnt ` `=` `0` ` ` ` ` `# To store the count of pairs of sticks ` ` ` `# with equal lengths ` ` ` `pairs ` `=` `0` ` ` ` ` `# Back-traverse and count ` ` ` `# the number of triangles ` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` ` ` `# Count the number of pairs ` ` ` `pairs ` `+` `=` `a[i] ` `/` `/` `2` ` ` ` ` `# If we have one remaining stick ` ` ` `# and we have a pair ` ` ` `if` `(a[i] ` `%` `2` `=` `=` `1` `and` `pairs > ` `0` `): ` ` ` ` ` `# Count 1 triangle ` ` ` `cnt ` `+` `=` `1` ` ` ` ` `# Reduce one pair ` ` ` `pairs ` `-` `=` `1` ` ` ` ` `# Count the remaining triangles ` ` ` `# that can be formed ` ` ` `cnt ` `+` `=` `(` `2` `*` `pairs) ` `/` `/` `3` ` ` `return` `cnt ` ` ` `# Driver code ` `a ` `=` `[` `1` `, ` `2` `, ` `2` `, ` `2` `, ` `2` `] ` `n ` `=` `len` `(a) ` `print` `(countTriangles(a, n)) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// number of positive area triangles ` `static` `int` `countTriangles(` `int` `a[], ` `int` `n) ` `{ ` ` ` ` ` `// To store the count of ` ` ` `// total triangles ` ` ` `int` `cnt = ` `0` `; ` ` ` ` ` `// To store the count of pairs of sticks ` ` ` `// with equal lengths ` ` ` `int` `pairs = ` `0` `; ` ` ` ` ` `// Back-traverse and count ` ` ` `// the number of triangles ` ` ` `for` `(` `int` `i = n - ` `1` `; i >= ` `0` `; i--) ` ` ` `{ ` ` ` ` ` `// Count the number of pairs ` ` ` `pairs += a[i] / ` `2` `; ` ` ` ` ` `// If we have one remaining stick ` ` ` `// and we have a pair ` ` ` `if` `(a[i] % ` `2` `== ` `1` `&& pairs > ` `0` `) ` ` ` `{ ` ` ` ` ` `// Count 1 triangle ` ` ` `cnt += ` `1` `; ` ` ` ` ` `// Reduce one pair ` ` ` `pairs -= ` `1` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Count the remaining triangles ` ` ` `// that can be formed ` ` ` `cnt += (` `2` `* pairs) / ` `3` `; ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a[] = { ` `1` `, ` `2` `, ` `2` `, ` `2` `, ` `2` `}; ` ` ` `int` `n = a.length; ` ` ` `System.out.println(countTriangles(a, n)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Split the array into odd number of segments of odd lengths
- Minimum number of given powers of 2 required to represent a number
- Representation of a number in powers of other
- Number of triangles after N moves
- Count the number of possible triangles
- Check if a number can be represented as sum of non zero powers of 2
- Count number of right triangles possible with a given perimeter
- Count number of unique Triangles using STL | Set 1 (Using set)
- Number of triangles that can be formed with given N points
- Number of Triangles in an Undirected Graph
- Balance pans using given weights that are powers of a number
- Form a number using corner digits of powers
- Number of possible Triangles in a Cartesian coordinate system
- Number of triangles in a plane if no more than two points are collinear
- Number of triangles formed from a set of points on three lines

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.