# Total number of BSTs using array elements

Prerequisite: Total number of possible Binary Search Trees with n keys
Given an array arr[] of N integers. The task is to count the number of Binary Search Tree can be made using each node of element in arr[] as a root node.

Examples:

Input: arr[] = { 20, 10, 30 }
Output: 1 2 2

Input: arr[] = { 1, 2, 3, 4, 5 }
Output: 14 5 4 5 14

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
The total number of possible Binary Search Tree(BST) is given by Catalan Number:

```Cn = (2n)!/(( n+1)!*n!)
where n = number of distinct keys.
```
For every element in arr[]:

1. Count the number of element(say c1) less than the current node.
2. Count the number of element(say c2) greater than the current node.
3. Then total number of Binary Search Tree(BST) can be formed using current element as a root node is equals to the product of total number of BST formed using c1 elements and otal number of BST formed using c2 elements.
```Total Number of BST = Cc1*Cc2
```

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above code  ` `#include ` `using` `namespace` `std; ` ` `  `// A function to find factorial of a  ` `// given number  ` `int` `fact(``int` `num)  ` `{  ` `    ``int` `fact = 1;  ` `     `  `    ``while``(num > 1)  ` `    ``{  ` `        ``fact *= num;  ` `        ``num -= 1;  ` `    ``}  ` `    ``return` `fact;  ` `}  ` ` `  `// Find nth catalan number  ` `int` `catalan(``int` `n)  ` `{  ` `    ``return` `fact(2 * n)/(fact(n) * fact(n + 1)) ;  ` `}  ` ` `  `// Driver Code  ` `int` `main()  ` `{  ` `     `  `    ``// size of arr[]  ` `    ``int` `n = 5;  ` `     `  `    ``// Elements in arr[]  ` `    ``int` `arr[] = {1, 2, 3, 4, 5};  ` `    ``int` `i,k;  ` `    ``for``(k = 0; k < n; k++)  ` `    ``{  ` `        ``int` `s = 0;  ` `     `  `        ``// Count the number of element  ` `        ``// less than current element  ` `        ``// in arr[k]  ` `        ``for``(i = 0; i < n; i++)  ` `        ``{  ` `            ``if` `(arr[i] < arr[k])  ` `                ``s += 1 ;  ` `        ``}  ` ` `  `        ``// Here s = number of node in left  ` `        ``// BST and (n-s-1) = number of node  ` `        ``// in right BST  ` `        ``// Find number of BST using elements  ` `        ``// in left BST  ` `        ``int` `catalan_leftBST = catalan(s) ;  ` `         `  `        ``// Find number of BST using elements  ` `        ``// in right BST  ` `        ``int` `catalan_rightBST = catalan(n - s - 1) ;  ` `         `  `        ``// Find total number of BST  ` `        ``int` `totalBST = catalan_rightBST * catalan_leftBST ;  ` `         `  `        ``// Print total BST count  ` `        ``cout<< totalBST <<  ``" "` `;  ` ` `  `    ``}  ` `}  ` ` `  `// This code is contributed by AnkitRai01  `

## Java

 `// java implementation of the above code ` `public` `class` `GFG  ` `{ ` `     `  `// A function to find factorial of a  ` `// given number  ` `static` `int` `fact(``int` `num) ` `{ ` `    ``int` `fact = ``1``;  ` `     `  `    ``while``(num > ``1``) ` `    ``{ ` `        ``fact *= num;  ` `        ``num -= ``1``;  ` `    ``} ` `    ``return` `fact;  ` `    ``} ` ` `  `// Find nth catalan number  ` `static` `int` `catalan(``int` `n) ` `{ ` `    ``return` `fact(``2` `* n)/(fact(n) * fact(n + ``1``)) ; ` `} ` ` `  `// Driver Code  ` `public` `static` `void` `main (String[] args)  ` `{ ` `     `  `    ``// size of arr[]  ` `    ``int` `n = ``5``; ` `     `  `    ``// Elements in arr[]  ` `    ``int` `arr[] = {``1``, ``2``, ``3``, ``4``, ``5``}; ` `    ``int` `i,k; ` `    ``for``(k = ``0``; k < n; k++) ` `    ``{  ` `        ``int` `s = ``0``; ` `     `  `        ``// Count the number of element  ` `        ``// less than current element  ` `        ``// in arr[k]  ` `        ``for``(i = ``0``; i < n; i++) ` `        ``{ ` `            ``if` `(arr[i] < arr[k]) ` `                ``s += ``1` `; ` `        ``} ` ` `  `        ``// Here s = number of node in left  ` `        ``// BST and (n-s-1) = number of node  ` `        ``// in right BST  ` `        ``// Find number of BST using elements  ` `        ``// in left BST  ` `        ``int` `catalan_leftBST = catalan(s) ; ` `         `  `        ``// Find number of BST using elements  ` `        ``// in right BST  ` `        ``int` `catalan_rightBST = catalan(n - s - ``1``) ; ` `         `  `        ``// Find total number of BST  ` `        ``int` `totalBST = catalan_rightBST * catalan_leftBST ; ` `         `  `        ``// Print total BST count  ` `        ``System.out.print(totalBST + ``" "``) ;  ` ` `  `    ``} ` `} ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# A function to find factorial of a  ` `# given number  ` `def` `fact(num):  ` `    ``fact ``=` `1``;  ` `     `  `    ``while``(num>``1``):  ` `        ``fact ``=` `fact ``*` `num;  ` `        ``num ``=` `num``-``1``;  ` `    ``return` `fact;  ` ` `  `# Find nth catalan number ` `def` `catalan(n): ` `    ``return` `fact(``2` `*` `n)``/``/``(fact(n)``*``fact(n ``+` `1``)) ` ` `  `# Driver Code  ` `if` `__name__ ``=``=` `'__main__'``: ` `     `  `    ``# size of arr[] ` `    ``n ``=` `5` `     `  `    ``# Elements in arr[] ` `    ``arr ``=` `[``1``, ``2``, ``3``, ``4``, ``5``] ` ` `  `    ``for` `k ``in` `range``(n): ` `        ``s ``=` `0` `     `  `        ``# Count the number of element ` `        ``# less than current element  ` `        ``# in arr[k] ` `        ``for` `i ``in` `range``(n): ` `            ``if` `arr[i] < arr[k]: ` `                ``s``+``=` `1` ` `  `        ``# Here s = number of node in left ` `        ``# BST and (n-s-1) = number of node  ` `        ``# in right BST ` `        ``# Find number of BST using elements  ` `        ``# in left BST ` `        ``catalan_leftBST ``=` `catalan(s) ` `         `  `        ``# Find number of BST using elements ` `        ``# in right BST ` `        ``catalan_rightBST ``=` `catalan(n``-``s``-``1``) ` `         `  `        ``# Find total number of BST  ` `        ``totalBST ``=` `catalan_rightBST ``*` `catalan_leftBST ` `         `  `        ``# Print total BST count ` `        ``print``(totalBST, end ``=``" "``) `

## C#

 `// C# implementation of the above code ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `      `  `// A function to find factorial of a  ` `// given number  ` `static` `int` `fact(``int` `num) ` `{ ` `    ``int` `fact = 1;  ` `      `  `    ``while``(num > 1) ` `    ``{ ` `        ``fact *= num;  ` `        ``num -= 1;  ` `    ``} ` `    ``return` `fact;  ` `    ``} ` `  `  `// Find nth catalan number  ` `static` `int` `catalan(``int` `n) ` `{ ` `    ``return` `fact(2 * n)/(fact(n) * fact(n + 1)) ; ` `} ` `  `  `// Driver Code  ` `public` `static` `void` `Main(String[] args)  ` `{ ` `      `  `    ``// size of []arr  ` `    ``int` `n = 5; ` `      `  `    ``// Elements in []arr  ` `    ``int` `[]arr = {1, 2, 3, 4, 5}; ` `    ``int` `i,k; ` `    ``for``(k = 0; k < n; k++) ` `    ``{  ` `        ``int` `s = 0; ` `      `  `        ``// Count the number of element  ` `        ``// less than current element  ` `        ``// in arr[k]  ` `        ``for``(i = 0; i < n; i++) ` `        ``{ ` `            ``if` `(arr[i] < arr[k]) ` `                ``s += 1 ; ` `        ``} ` `  `  `        ``// Here s = number of node in left  ` `        ``// BST and (n-s-1) = number of node  ` `        ``// in right BST  ` `        ``// Find number of BST using elements  ` `        ``// in left BST  ` `        ``int` `catalan_leftBST = catalan(s) ; ` `          `  `        ``// Find number of BST using elements  ` `        ``// in right BST  ` `        ``int` `catalan_rightBST = catalan(n - s - 1) ; ` `          `  `        ``// Find total number of BST  ` `        ``int` `totalBST = catalan_rightBST * catalan_leftBST ; ` `          `  `        ``// Print total BST count  ` `        ``Console.Write(totalBST + ``" "``) ;  ` `  `  `    ``} ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```14 5 4 5 14
```

Time Complexity: O(N2)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : AnkitRai01, 29AjayKumar