-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFG Count the Number of Full Binary Trees
63 lines (38 loc) · 1.52 KB
/
GFG Count the Number of Full Binary Trees
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution{
public:
long long int numoffbt(long long int arr[], int n){
// Time complexity O(n*sqrt(n))
// Space complexity O(n)
// To reduce the time complexity instead of sorting
long long int mini = 1e9;
long long int maxi = -1e9;
int m[int(1e5+1)];
memset(m, 0, sizeof(m));
for ( int i = 0; i<n; i++ ){
m[arr[i]] = 1;
mini = min(arr[i], mini);
maxi = max(arr[i], maxi);
}
// --------------------------------------------------------------------------------------
long int mod = 1000000007;
long int ans = 0;
for ( int i = mini; i<=maxi; i++ ){
if ( m[i] == 0 )
continue;
if ( m[i] != 1 )
continue;
for ( int j = mini; j*j<= i; j++ ){
if ( i % j == 0 && m[i/j] != 0 && m[j] != 0 ){
if ( i/j != j )
m[i] += 2*(m[i/j] * m[j]);
else
m[i] += m[j]*m[j];
m[i] %= mod;
}
}
ans += m[i];
ans %= mod;
}
return ans;
}
};