-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_ways_to_form_max_heap.cpp
More file actions
281 lines (208 loc) · 6.54 KB
/
count_ways_to_form_max_heap.cpp
File metadata and controls
281 lines (208 loc) · 6.54 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
Suppose there are n distinct elements to be used in Max heap. Now it is for sure that the maximum element among this n distinct element will surely be placed on the root of the heap.
Now there will be remaining (n-1) elements to be arranged.
Now point to be remembered here is that the structure of the heap will remain the same. That is only the values within the node will change however the overall structure remaining the same.
As structure of the heap remains the same, the number of elements that are present in the left sub-tree of the root (L) will be fixed. And similarly the number of the elements on the right sub-tree (R) of the root. And also following equality holds .
L + R = (n-1)
Now as all the remaining (n-1) elements are less than the element present at the root(The Max Element), whichever L elements among ‘n-1` elements we put in the left sub-tree, it doesn’t matter because it will satisfy the Max Heap property.
So now there are (n-1)CL ways to pickup L elements from (n-1) elements. And then recurse the solution.
So final equation will be as follows :
(n-1)CL * getNumberOfMaxHeaps(L) * getNumberOfMaxHeaps(R)
So now the question remains only of finding L for given n. It can be found as follows:
Find the height of the heap h = log2(n)
Find the max number of elements that can be present in the hth level of any heap . Lets call it m. m = 2h
Find the number of elements that are actually present in last level(hth level) in heap of size n. Lets call it p. p = n - (2h - 1)
Now if the last level of the heap is more than or equal to exactly half filled, then L would be 2h - 1
However if it is half filled then it will reduced by the number of elements in last level left to fill exactly half of the last level.
So final equation for L will be as follows :
L = 2h - 1 if p >= m/2
= 2h - 1 - (m/2 - p) if p<(m/2)
// CPP program to count max heaps with n distinct keys
#include <iostream>
using namespace std;
#define MAXN 105 // maximum value of n here
// dp[i] = number of max heaps for i distinct integers
int dp[MAXN];
// nck[i][j] = number of ways to choose j elements
// form i elements, no order */
int nck[MAXN][MAXN];
// log2[i] = floor of logarithm of base 2 of i
int log2[MAXN];
// to calculate nCk
int choose(int n, int k)
{
if (k > n)
return 0;
if (n <= 1)
return 1;
if (k == 0)
return 1;
if (nck[n][k] != -1)
return nck[n][k];
int answer = choose(n - 1, k - 1) + choose(n - 1, k);
nck[n][k] = answer;
return answer;
}
// calculate l for give value of n
int getLeft(int n)
{
if (n == 1)
return 0;
int h = log2[n];
// max number of elements that can be present in the
// hth level of any heap
int numh = (1 << h); //(2 ^ h)
// number of elements that are actually present in
// last level(hth level)
// (2^h - 1)
int last = n - ((1 << h) - 1);
// if more than half-filled
if (last >= (numh / 2))
return (1 << h) - 1; // (2^h) - 1
else
return (1 << h) - 1 - ((numh / 2) - last);
}
// find maximum number of heaps for n
int numberOfHeaps(int n)
{
if (n <= 1)
return 1;
if (dp[n] != -1)
return dp[n];
int left = getLeft(n);
int ans = (choose(n - 1, left) * numberOfHeaps(left)) *
(numberOfHeaps(n - 1 - left));
dp[n] = ans;
return ans;
}
// function to initialize arrays
int solve(int n)
{
for (int i = 0; i <= n; i++)
dp[i] = -1;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
nck[i][j] = -1;
int currLog2 = -1;
int currPower2 = 1;
// for each power of two find logarithm
for (int i = 1; i <= n; i++) {
if (currPower2 == i) {
currLog2++;
currPower2 *= 2;
}
log2[i] = currLog2;
}
return numberOfHeaps(n);
}
// driver function
int main()
{
int n = 10;
cout << solve(n) << endl;
return 0;
}
// #include <bits/stdc++.h>
// using namespace std;
// long long int facto(int n){
// if(n==1){
// return 1;
// }
// return n*facto(n-1);
// }
// long long int get_comb(int n, k){
// if(n-k < k){
// k = n-k;
// }
// long long int num = 1;
// for(int i=0; i<k; i++){
// num *= n;
// n--;
// }
// long long int den = facto(k);
// return num/den;
// }
// long long int rec(int n){
// if(n==1){
// return 1;
// }
// return get_comb(n)
// }
// void count_ways_to_form_max_heap(int n){
// long long int count = rec(n);
// }
// int main()
// {
// int test;
// cin>>test;
// while(test--){
// int n;
// cin>>n;
// count_ways_to_form_max_heap(n);
// }
// return 0;
// }
//
#define MAXN 105
long long int dp[MAXN]; /* dp[i] = number of max heaps for i distinct integers */
long long int nck[MAXN][MAXN]; /* nck[i][j] = i choose j if i>=j else 0 */
int log_2[MAXN]; /* log_2[i] = int(log_2 base 2 of i) */
long long int MOD = 1000000007;
long long int choose(int n,int k)
{
if(k>n)
return 0;
if(n<=1)
return 1;
if(k==0)
return 1;
if(nck[n][k]!=-1)
return nck[n][k];
long long int answer = choose(n-1,k-1) + choose(n-1,k);
answer%=MOD;
nck[n][k] = answer;
return answer;
}
int getL(int n)
{
if(n==1)
return 0;
int h = log_2[n];
int p = n - ((1<<(h)) - 1);
int m = (1<<h);
if(p>=(m/2))
return (1<<(h)) - 1;
else
return (1<<(h)) - 1 - ((m/2) - p);
}
long long int getNumberOfMaxHeaps(int n)
{
if(n<=1)
return 1;
if(dp[n]!=-1)
return dp[n];
int L = getL(n);
long long int ans = (choose(n-1,L)*getNumberOfMaxHeaps(L))%MOD*(getNumberOfMaxHeaps(n-1-L));
ans%=MOD;
dp[n] = ans;
return ans;
}
int Solution::solve(int n)
{
for(int i=0;i<=n;i++)
dp[i]=-1;
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
nck[i][j] = -1;
int currlog_2Answer = -1;
int currPower2 = 1;
for(int i=1;i<=n;i++)
{
if(currPower2==i)
{
currlog_2Answer++;
currPower2*=2;
}
log_2[i] = currlog_2Answer;
}
return (int)getNumberOfMaxHeaps(n);
}