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 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 trianglesint 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 codeint 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 approachclass GFG{Â Â Â Â Â // Function to return the// number of positive area trianglesstatic 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 codepublic 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 trianglesdef 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 codea = [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 trianglesFunction 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 trianglesfunction 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 codevar a = [ 1, 2, 2, 2, 2 ];var n = a.length;Â
document.write(countTriangles(a, n));Â
// This code is contributed by aashish1995Â
</script> |
3
Â
Time Complexity: O(n)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



