You are given an array 'ARR' of positive integers. Your task is to check whether the array 'ARR' is a valid Preorder Traversal of a Binary Search Tree.
A binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree whose internal nodes each store a value greater than or equal all the values in the node's left subtree and less than those in its right subtree.
For ExampleConsider the array ARR = [ 7, 4, 8 ] having 3 elements.
This array represents the pre-order traversal of the below tree.

Hence, the above array 'ARR' is a valid Preorder Traversal of a Binary Search Tree.
The first line of the 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 array 'ARR'.
The second line of each test case contains 'N' space-separated integers denoting the array elements.
Output Format
For each test case, print 1 if the array represents a valid preorder traversal of a Binary Search Tree. Otherwise, print 0.
Print the output of each test case in a new line.
Note
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
0 <= ARR[i] <= 10^9
Time Limit: 1 sec
2
7
4 2 1 3 6 5 7
4
5 4 6 4
1
0
For the first test case :
Consider the below Binary Search Tree
Its preorder traversal is [ 4, 2, 1, 3, 6, 5, 7 ] which is equal to the input array 'ARR'.
Therefore, the answer is 1 in this case.
For the second test case :
It can be seen that for the array ARR = [ 5, 4, 6, 4 ], there does not exist a Binary Search Tree for which the above array can represent its pre-order traversal.
2
3
6 2 6
4
5 6 1 4
1
0
Try to think of a recursive approach to divide the array into left subtree and right subtree and check for them independently.
The idea is to find the first node in the array having a value greater than the array's first element, i.e., its root. Let ARR[i] be that element. Then we will treat the elements to the right of ARR[i], including ARR[i] itself as the right subtree and all the elements before ARR[i] as the left subtree.
Now we will recursively check whether the right subtree and left subtree represent a valid Preorder traversal of a Binary Search Tree independently. We will also maintain two variables, upperBound and lowerBound, to denote the maximum possible and minimum possible that a node can contain. We will initialize the lowerBound as INT_MIN and upperBound as INT_MAX. Whenever we visit a left subtree, we will update the upperBound to the data contained on the root of the tree, and lowerBound will remain unchanged, and whenever we visit a right subtree we will update the lowerBound to the data contained on the root of the tree and upperBound will remain unchanged.
Algorithm
In the worst case, when the array is sorted in decreasing order, there will be N recursive calls, and each recursive call takes O(N) time to find the firstGreaterIndex variable. Hence, the overall time complexity will be O(N^2).
In the worst case, when the array is sorted in decreasing order, there will be N recursive calls, and the corresponding recursion depth will be N. Hence, the overall space complexity will be O(N).