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.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
9 votes
Tags:
AlgorithmsBasic ProgrammingString Manipulation
Points:30
3 votes
Tags:
Basic ProgrammingBasics of ImplementationHash MapsImplementationMedium
Points:30
6 votes
Tags:
AlgorithmsBasic ProgrammingBasics of ImplementationImplementationMedium