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++
#include <bits/stdc++.h>
using namespace std;
int countTriangles( int a[], int n)
{
int cnt = 0;
int pairs = 0;
for ( int i = n - 1; i >= 0; i--) {
pairs += a[i] / 2;
if (a[i] % 2 == 1 && pairs > 0) {
cnt += 1;
pairs -= 1;
}
}
cnt += (2 * pairs) / 3;
return cnt;
}
int main()
{
int a[] = { 1, 2, 2, 2, 2 };
int n = sizeof (a) / sizeof (a[0]);
cout << countTriangles(a, n);
return 0;
}
|
Java
class GFG
{
static int countTriangles( int a[], int n)
{
int cnt = 0 ;
int pairs = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--)
{
pairs += a[i] / 2 ;
if (a[i] % 2 == 1 && pairs > 0 )
{
cnt += 1 ;
pairs -= 1 ;
}
}
cnt += ( 2 * pairs) / 3 ;
return cnt;
}
public static void main(String[] args)
{
int a[] = { 1 , 2 , 2 , 2 , 2 };
int n = a.length;
System.out.println(countTriangles(a, n));
}
}
|
Python3
def countTriangles(a, n):
cnt = 0
pairs = 0
for i in range (n - 1 , - 1 , - 1 ):
pairs + = a[i] / / 2
if (a[i] % 2 = = 1 and pairs > 0 ):
cnt + = 1
pairs - = 1
cnt + = ( 2 * pairs) / / 3
return cnt
a = [ 1 , 2 , 2 , 2 , 2 ]
n = len (a)
print (countTriangles(a, n))
|
C#
using System;
class GFG
{
static int countTriangles( int []a, int n)
{
int cnt = 0;
int pairs = 0;
for ( int i = n - 1; i >= 0; i--)
{
pairs += a[i] / 2;
if (a[i] % 2 == 1 && pairs > 0)
{
cnt += 1;
pairs -= 1;
}
}
cnt += (2 * pairs) / 3;
return cnt;
}
public static void Main()
{
int []a = { 1, 2, 2, 2, 2 };
int n = a.Length;
Console.WriteLine(countTriangles(a, n));
}
}
|
PHP
<?php
Function countTriangles( $a , $n )
{
$cnt = 0;
$pairs = 0;
for ( $i = $n - 1; $i >= 0; $i --)
{
$pairs += $a [ $i ] / 2;
if ( $a [ $i ] % 2 == 1 && $pairs > 0)
{
$cnt += 1;
$pairs -= 1;
}
}
$cnt += (int)((2 * $pairs ) / 3);
return $cnt ;
}
$a = array (1, 2, 2, 2, 2 );
$n = sizeof( $a );
echo (countTriangles( $a , $n ));
?>
|
Javascript
<script>
function countTriangles(a , n)
{
var cnt = 0;
var pairs = 0;
for (let i = n - 1; i >= 0; i--)
{
pairs += a[i] / 2;
if (a[i] % 2 == 1 && pairs > 0)
{
cnt += 1;
pairs -= 1;
}
}
cnt += parseInt((2 * pairs) / 3);
return cnt;
}
var a = [ 1, 2, 2, 2, 2 ];
var n = a.length;
document.write(countTriangles(a, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
08 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...