You are given a sequence array 'ARR' of ‘N’ integers. For each ARR[i] where 0 <= ‘i’ < 'N' , your task is to find the number of Binary search trees(BST) possible with elements of sequence ARR as nodes and ARR[i] as the root node.
The answer could be very large,so output your answer modulo (10 ^ 9 + 7). Also, use modular division when required.
For Example:
Let the sequence be 1, 4, 5, 6, 7 then return 14 5 4 5 14 i.e when 1 is the root node the number of BSTs possible is 14. Similarly, when 4 is the root node the number of BST’s possible is 5. In a similar way, we calculate the number of BST’s possible for the remaining elements of the sequence.
Note:
1. Consider 0 based Indexing.
2. A Binary Search Tree is a special kind of tree in which each node of the tree has at most 2 children and for every node, the value of nodes of the left subtree of any node is less than the current node and value of nodes of the right subtree are greater than the current node.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains an integer ‘N’ denoting the number of elements in the given sequence.
The next line contains ‘N’ space-separated integers denoting the sequence ‘ARR’.
Output Format:
For each test case, print a single line containing 'N' space-separated which represents the number of BST’s that can be formed using each element of the given sequence as the root node.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10 ^ 3
- 10 ^ 9 <= ARR[i] <= 10 ^ 9
Where ‘T’ is the total number of test cases, ‘N’ denotes the number of elements in the sequence.
Time limit: 1 sec.
2
3
1 2 3
3
20 30 10
2 1 2
1 2 2
Test case 1:
We will have the following trees :
With 1 as root node:

The above 2 BST are possible.
With 2 as the root node :

Only this one BST is possible.
With 3 as the root node:

The above 2 BSTs are possible.
Hence we return the sequence '2 1 2'.
2
5
1 2 3 4 5
6
2 6 7 3 8 5
14 5 4 5 14
42 10 14 14 42 10
Test Case 1: For the first test case, with 1 as the root node we will have 14 unique BSTs. Similarly for 2,3,4,5 as root nodes, we will have 5,4,5,14 unique BSTs respectively.
Test Case 2: For the second test case, with 2 as the root node we will have 42 unique BSTs. Similarly for 6,7,3,8,5 as root nodes, we will have 10,14,14,42,10 unique BSTs respectively.
Find all possible Binary Search trees for each element using Catalan Number.
Keeping this in mind we can write the following solution:
O(N ^ 2), where ‘N’ denotes the number of elements present in the sequence.
Each iteration will take order of ‘N’ time as we need to iterate through total n elements and find the Catalan number which takes an order of ‘N’ time. Hence the overall time complexity is the order of N ^ 2.
O(N), where ‘N’ denotes the number of elements present in the sequence.
Output takes a space of order of ‘N’ as we need an array of 'N' elements to store the output sequence.