Given an array of n integers, each integer is greater than 1. The task is to find the number of Full binary tree from the given integers, such that each non-leaf node value is the product of its children value. Given that, each integer can be used multiple times in a full binary tree.
Examples:
Input : arr[] = { 2, 3, 4, 6 }.
Output : 7
There can be 7 full binary tree with the given product property.
// Four trees with single nodes
2 3 4 6
// Three trees with three nodes
4 ,
/ \
2 2
6 ,
/ \
2 3
6
/ \
3 2
We find maximum value in given array and create an array to store presence of elements in this array. The idea is, for all multiples of each integer less than the maximum value of the array, try to make full binary tree if the multiple is present in the array.
Observe that for any full binary tree with given property, the smaller values will always be at the last level. So, try to find the number of such full binary tree from the minimum value of the array to maximum value of the array.
Algorithm:
- Initialize possible number of such full binary tree for each element equal to 1. Since single node also contribute to the answer.
- For each element of the array, arr[i], from minimum value to maximum value of array.
- For each multiple of arr[i], find if multiple is present or not.
- If yes, then the number of such possible full binary tree for multiple of arr[i], say m, is equal to the product of the number of such possible full binary tree of arr[i] and number of such possible full binary tree of arr[i]/m.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int numoffbt( int arr[], int n)
{
int maxvalue = INT_MIN, minvalue = INT_MAX;
for ( int i = 0; i < n; i++)
{
maxvalue = max(maxvalue, arr[i]);
minvalue = min(minvalue, arr[i]);
}
int mark[maxvalue + 2];
int value[maxvalue + 2];
memset (mark, 0, sizeof (mark));
memset (value, 0, sizeof (value));
for ( int i = 0; i < n; i++)
{
mark[arr[i]] = 1;
value[arr[i]] = 1;
}
int ans = 0;
for ( int i = minvalue; i <= maxvalue; i++)
{
if (mark[i])
{
for ( int j = i + i;
j <= maxvalue && j/i <= i; j += i)
{
if (!mark[j])
continue ;
value[j] = value[j] + (value[i] * value[j/i]);
if (i != j/i)
value[j] = value[j] + (value[i] * value[j/i]);
}
}
ans += value[i];
}
return ans;
}
int main()
{
int arr[] = { 2, 3, 4, 6 };
int n = sizeof (arr)/ sizeof (arr[0]);
cout << numoffbt(arr, n) << endl;
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static int numoffbt( int arr[], int n)
{
int maxvalue = - 2147483647 ;
int minvalue = 2147483647 ;
for ( int i = 0 ; i < n; i++)
{
maxvalue = Math.max(maxvalue, arr[i]);
minvalue = Math.min(minvalue, arr[i]);
}
int mark[] = new int [maxvalue + 2 ];
int value[] = new int [maxvalue + 2 ];
Arrays.fill(mark, 0 );
Arrays.fill(value, 0 );
for ( int i = 0 ; i < n; i++)
{
mark[arr[i]] = 1 ;
value[arr[i]] = 1 ;
}
int ans = 0 ;
for ( int i = minvalue; i <= maxvalue; i++)
{
if (mark[i] != 0 )
{
for ( int j = i + i;
j <= maxvalue && j/i <= i; j += i)
{
if (mark[j] == 0 )
continue ;
value[j] = value[j] + (value[i]
* value[j/i]);
if (i != j / i)
value[j] = value[j] + (value[i]
* value[j/i]);
}
}
ans += value[i];
}
return ans;
}
public static void main (String[] args)
{
int arr[] = { 2 , 3 , 4 , 6 };
int n = arr.length;
System.out.print(numoffbt(arr, n));
}
}
|
Python3
def numoffbt(arr, n):
maxvalue = - 2147483647
minvalue = 2147483647
for i in range (n):
maxvalue = max (maxvalue, arr[i])
minvalue = min (minvalue, arr[i])
mark = [ 0 for i in range (maxvalue + 2 )]
value = [ 0 for i in range (maxvalue + 2 )]
for i in range (n):
mark[arr[i]] = 1
value[arr[i]] = 1
ans = 0
for i in range (minvalue, maxvalue + 1 ):
if (mark[i] ! = 0 ):
j = i + i
while (j < = maxvalue and j / / i < = i):
if (mark[j] = = 0 ):
continue
value[j] = value[j] + (value[i] * value[j / / i])
if (i ! = j / / i):
value[j] = value[j] + (value[i] * value[j / / i])
j + = i
ans + = value[i]
return ans
arr = [ 2 , 3 , 4 , 6 ]
n = len (arr)
print (numoffbt(arr, n))
|
C#
using System;
class GFG
{
static int numoffbt( int []arr, int n)
{
int maxvalue = -2147483647, minvalue = 2147483647;
for ( int i = 0; i < n; i++)
{
maxvalue = Math.Max(maxvalue, arr[i]);
minvalue = Math.Min(minvalue, arr[i]);
}
int []mark= new int [maxvalue + 2];
int []value= new int [maxvalue + 2];
for ( int i = 0;i < maxvalue + 2; i++)
{
mark[i]=0;
value[i]=0;
}
for ( int i = 0; i < n; i++)
{
mark[arr[i]] = 1;
value[arr[i]] = 1;
}
int ans = 0;
for ( int i = minvalue; i <= maxvalue; i++)
{
if (mark[i] != 0)
{
for ( int j = i + i;
j <= maxvalue && j/i <= i; j += i)
{
if (mark[j] == 0)
continue ;
value[j] = value[j] + (value[i] * value[j/i]);
if (i != j/i)
value[j] = value[j] + (value[i] * value[j/i]);
}
}
ans += value[i];
}
return ans;
}
public static void Main()
{
int []arr = { 2, 3, 4, 6 };
int n = arr.Length;
Console.Write(numoffbt(arr, n));
}
}
|
Javascript
<script>
function numoffbt(arr, n) {
let maxvalue = Number.MIN_SAFE_INTEGER, minvalue = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < n; i++) {
maxvalue = Math.max(maxvalue, arr[i]);
minvalue = Math.min(minvalue, arr[i]);
}
let mark = new Array(maxvalue + 2).fill(0);
let value = new Array(maxvalue + 2).fill(0);
for (let i = 0; i < n; i++) {
mark[arr[i]] = 1;
value[arr[i]] = 1;
}
let ans = 0;
for (let i = minvalue; i <= maxvalue; i++) {
if (mark[i]) {
for (let j = i + i;
j <= maxvalue && j / i <= i; j += i) {
if (!mark[j])
continue ;
value[j] = value[j] + (value[i] * value[j / i]);
if (i != j / i)
value[j] = value[j] + (value[i] * value[j / i]);
}
}
ans += value[i];
}
return ans;
}
let arr = [2, 3, 4, 6];
let n = arr.length;
document.write(numoffbt(arr, n) + "<br>" );
</script>
|
Time Complexity: O(N^2) ,where N is the number of nodes in given binary tree.
Auxiliary Space: O(N)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Sep, 2023
Like Article
Save Article