Number of triangles possible with given lengths of sticks which are powers of 2
Last Updated :
08 Jun, 2022
Given an array of N integers where arr[i] denotes the number of sticks of length 2i. 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:
(20, 24, 24), (21, 23, 23), (21, 22, 22).
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;
}
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.
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
C#
// C# implementation of the approach
using System;
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()
{
int []a = { 1, 2, 2, 2, 2 };
int n = a.Length;
Console.WriteLine(countTriangles(a, n));
}
}
// This code is contributed by Ryuga
PHP
<?php
// PHP implementation of the approach
// Function to return the
// number of positive area triangles
Function 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 = $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 += (int)((2 * $pairs) / 3);
return $cnt;
}
// Driver code
$a = array(1, 2, 2, 2, 2 );
$n = sizeof($a);
echo(countTriangles($a, $n));
// This code is contributed by Code_Mech.
?>
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the number
// of positive area triangles
function countTriangles(a , n)
{
// To store the count of
// total triangles
var cnt = 0;
// To store the count of pairs
// of sticks with equal lengths
var pairs = 0;
// Back-traverse and count
// the number of triangles
for(let 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 += parseInt((2 * pairs) / 3);
return cnt;
}
// Driver code
var a = [ 1, 2, 2, 2, 2 ];
var n = a.length;
document.write(countTriangles(a, n));
// This code is contributed by aashish1995
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Count number of triangles possible with length of sides not exceeding N Given an integer N, the task is to find the total number of right angled triangles that can be formed such that the length of any side of the triangle is at most N. A right-angled triangle satisfies the following condition: X2 + Y2 = Z2 where Z represents the length of the hypotenuse, and X and Y re
10 min read
Number of triangles that can be formed with given N points Given X and Y coordinates of N points on a Cartesian plane. The task is to find the number of possible triangles with the non-zero area that can be formed by joining each point to every other point. Examples: Input: P[] = {{0, 0}, {2, 0}, {1, 1}, {2, 2}} Output: 3 Possible triangles can be [(0, 0},
9 min read
Count number of right triangles possible with a given perimeter Given a perimeter P, the task is to find the number of right triangles possible with perimeter equal to p.Examples: Input: P = 12 Output: number of right triangles = 1 The only right angle possible is with sides hypotenuse = 5, perpendicular = 4 and base = 3. Input: p = 840 Output: number of right t
6 min read
Count number of triangles possible for the given sides range Given four integers A, B, C, and D, the task is to find the number of distinct sets (X, Y, and Z) where X, Y and Z denotes the length of sides forming a valid triangle. A ? X ? B, B ? Y ? C, and C ? Z ? D.Examples: Input: A = 2, B = 3, C = 4, D = 5 Output: 7 Explanation: Possible Length of Side of T
10 min read
Number of possible Triangles in a Cartesian coordinate system Given n points in a Cartesian coordinate system. Count the number of triangles formed. Examples: Input : point[] = {(0, 0), (1, 1), (2, 0), (2, 2) Output : 3 Three triangles can be formed from above points. A simple solution is to check if the determinant of the three points selected is non-zero or
7 min read