# Number of ways to form a heap with n distinct integers

• Difficulty Level : Expert
• Last Updated : 09 Jul, 2021

Given n, how many distinct Max Heap can be made from n distinct integers?
Examples:

```Input : n = 3
Output : Assume the integers are 1, 2, 3.
Then the 2 possible max heaps are:
3
/ \
1   2

3
/ \
2   1

Input : n = 4
Output : Assume the integers are 1, 2, 3, 4.
Then the 3 possible max heaps are:
4
/ \
3   2
/
1

4
/ \
2   3
/
1

4
/ \
3   1
/
2```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Since there is only one element as the root, it must be the largest number. Now we have n-1 remaining elements. The main observation here is that because of the max heap properties, the structure of the heap nodes will remain the same in all instances, but only the values in the nodes will change.
Assume there are l elements in the left sub-tree and r elements in the right sub-tree. Now for the root, l + r = n-1. From this we can see that we can choose any l of the remaining n-1 elements for the left sub-tree as they are all smaller than the root.
We know the there are ways to do this. Next for each instance of these, we can have many heaps with l elements and for each of those we can have many heaps with r elements. Thus we can consider them as subproblems and recur for the final answer as:
T(n) = * T(L) * T(R).
Now we have to find the values for l and r for a given n. We know that the height of the heap h = . Also the maximum number of elements that can be present in the h th level of any heap, m = , where the root is at the 0th level. Moreover the number of elements actually present in the last level of the heap p = n – ( – 1). (since number of nodes present till the penultimate level). Thus, there can be two cases: when the last level is more than or equal to half-filled:
l = – 1, if p >= m / 2
(or) the last level is less than half-filled:
l = – 1 – ((m / 2) – p), if p < m / 2
(we get – 1 here because left subtree has nodes.
From this we can also say that r = n – l – 1.
We can use the dynamic programming approach discussed in this post here to find the values of . Similarly if we look at the recursion tree for the optimal substructure recurrence formed above, we can see that it also has overlapping subproblems property, hence can be solved using dynamic programming:

```             T(7)
/    \
T(3)   T(3)
/  \     /  \
T(1)  T(1) T(1) T(1) ```

Following is the implementation of the above approach:

## C++

 `// CPP program to count max heaps with n distinct keys``#include ``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;``}`

## Java

 `// Java program to count max heaps with n distinct keys``class` `GFG``{` `    ``static` `int` `MAXN = ``105``; ``// maximum value of n here` `    ``// dp[i] = number of max heaps for i distinct integers``    ``static` `int``[] dp = ``new` `int``[MAXN];` `    ``// nck[i][j] = number of ways to choose j elements``    ``//         form i elements, no order */``    ``static` `int``[][] nck = ``new` `int``[MAXN][MAXN];` `    ``// log2[i] = floor of logarithm of base 2 of i``    ``static` `int``[] log2 = ``new` `int``[MAXN];` `    ``// to calculate nCk``    ``public` `static` `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``    ``public` `static` `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``    ``public` `static` `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``    ``public` `static` `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 code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n = ``10``;``        ``System.out.print(solve(n));``    ``}``}` `// This code has been contributed by 29AjayKumar`

## Python3

 `# Python program to count max heaps with n distinct keys` `MAXN ``=` `105` `# maximum value of n here` `# dp[i] = number of max heaps for i distinct integers``dp ``=` `[``0``]``*``MAXN` `# nck[i][j] = number of ways to choose j elements``#             form i elements, no order */``nck ``=` `[[``0` `for` `i ``in` `range``(MAXN)] ``for` `j ``in` `range``(MAXN)]` `# log2[i] = floor of logarithm of base 2 of i``log2 ``=` `[``0``]``*``MAXN` `# to calculate nCk``def` `choose(n, 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]` `    ``answer ``=` `choose(n ``-` `1``, k ``-` `1``) ``+` `choose(n ``-` `1``, k)``    ``nck[n][k] ``=` `answer``    ``return` `answer`  `# calculate l for give value of n``def` `getLeft(n):``    ``if` `(n ``=``=` `1``):``        ``return` `0` `    ``h ``=` `log2[n]` `    ``# max number of elements that can be present in the``    ``# hth level of any heap``    ``numh ``=` `(``1` `<< h) ``#(2 ^ h)` `    ``# number of elements that are actually present in``    ``# last level(hth level)``    ``# (2^h - 1)``    ``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``def` `numberOfHeaps(n):``    ``if` `(n <``=` `1``):``        ``return` `1` `    ``if` `(dp[n] !``=` `-``1``):``        ``return` `dp[n]` `    ``left ``=` `getLeft(n)``    ``ans ``=` `(choose(n ``-` `1``, left) ``*` `numberOfHeaps(left)) ``*` `(numberOfHeaps(n ``-` `1` `-` `left))``    ``dp[n] ``=` `ans``    ``return` `ans`  `# function to initialize arrays``def` `solve(n):``    ``for` `i ``in` `range``(n``+``1``):``        ``dp[i] ``=` `-``1` `    ``for` `i ``in` `range``(n``+``1``):``        ``for` `j ``in` `range``(n``+``1``):``            ``nck[i][j] ``=` `-``1` `    ``currLog2 ``=` `-``1``    ``currPower2 ``=` `1` `    ``# for each power of two find logarithm``    ``for` `i ``in` `range``(``1``,n``+``1``):``        ``if` `(currPower2 ``=``=` `i):``            ``currLog2 ``+``=` `1``            ``currPower2 ``*``=` `2``        ``log2[i] ``=` `currLog2``    ``return` `numberOfHeaps(n)`  `# Driver code``n ``=` `10``print``(solve(n))` `# This code is contributed by ankush_953`

## C#

 `// C# program to count max heaps with n distinct keys``using` `System;``  ` `class` `GFG``{``    ``static` `int` `MAXN = 105; ``// maximum value of n here``      ` `    ``// dp[i] = number of max heaps for i distinct integers``    ``static` `int``[] dp = ``new` `int``[MAXN]; ``      ` `    ``// nck[i][j] = number of ways to choose j elements``    ``//             form i elements, no order */``    ``static` `int``[,] nck = ``new` `int``[MAXN,MAXN]; ``      ` `    ``// log2[i] = floor of logarithm of base 2 of i``    ``static` `int``[] log2 = ``new` `int``[MAXN]; ``      ` `    ``// to calculate nCk``    ``public` `static` `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``    ``public` `static` `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``    ``public` `static` `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``    ``public` `static` `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``    ``static` `void` `Main()``    ``{``        ``int` `n = 10;``        ``Console.Write(solve(n));``    ``}``    ``//This code is contributed by DrRoot_``}`

## Javascript

 ``

Output:

`3360`

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.6479819,"math_prob":0.9951178,"size":12266,"snap":"2021-43-2021-49","text_gpt3_token_len":4352,"char_repetition_ratio":0.13912901,"word_repetition_ratio":0.5051737,"special_character_ratio":0.3978477,"punctuation_ratio":0.116324924,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99995816,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22],"im_url_duplicate_count":[null,4,null,4,null,4,null,10,null,10,null,2,null,10,null,10,null,10,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-01T03:01:39Z\",\"WARC-Record-ID\":\"<urn:uuid:57054a58-3840-4af2-890d-73bc478c0836>\",\"Content-Length\":\"223768\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:65557d77-3f3e-4cd4-b13e-d6c83feffe5e>\",\"WARC-Concurrent-To\":\"<urn:uuid:33d8022b-bd3b-40ea-b7a0-9c5a4a018fa0>\",\"WARC-IP-Address\":\"23.207.202.39\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/number-ways-form-heap-n-distinct-integers/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:B2VFO4M4DRWYNGWT5SHAIJXK3EFZA5SN\",\"WARC-Block-Digest\":\"sha1:NXMCR5ZSOIGQBGSUHSMM5UZI4QE7LYUH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964359082.78_warc_CC-MAIN-20211201022332-20211201052332-00535.warc.gz\"}"}