Ways to a BST
Practice
3.7 (3 votes)
Binary search tree
Combinatorics
Graphs
Medium
Problem
25% Success 1024 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given N distinct numbers in the form of an array \(Arr_i\). You can form a Binary Search Tree(BST) by inserting them in the order they are given.Now find the number of different permutations of the array, so that the BST formed in each permutation is same as that of the given array(including the given permutation). As the number can be large, output modulo \((10^9+7)\).
Constraints:
- \(1 ≤ T ≤ 10\)
- \(1 ≤ N ≤ 1000\)
- \(1 ≤ Arr_i ≤ 1000000000\)
Format of the input file:
First line : T i.e Number of testcases.
For each testcase :
First line : N.
Second line : N space separated integers denoting the array .
Format of the output file:
Output the answer to each testcase in a separate line.
Submissions
Please login to view your submissions
Similar Problems
Points:30
9 votes
Tags:
Mediumad hocapproved
Points:30
22 votes
Tags:
Basic ProgrammingC++
Points:20
19 votes
Tags:
ApprovedDepth First SearchEasyGraphsReady
Editorial