Leaderboard
Showing:
Handle Score
@Ibrahim Nash 5725
@blackshadows 5685
@akhayrutdinov 5111
@mb1973 4989
@Quandray 4908
@saiujwal13083 4156
@sanjay05 3762
@marius_valentin_dragoi 3511
@sushant_a 3453
@KshamaGupta 3318
@the_coder95 3302
Complete Leaderboard
Preorder Traversal and BST
Medium Accuracy: 46.05% Submissions: 12245 Points: 4

Given an array arr of size n, write a program that prints 1 if given array can represent preorder traversal of a BST, else prints 0.

Input:
The first line of input contains an integer T denoting the number of test cases. T testcases follow. Each testcase contains two lines of input.
The first line of each test case is n, denoting the size of array.
The second line of each test case contains n elements of the array.

Output:
For each testcase, in a new line, print 1 if the array represents preorder traversal else print 0.

Constraints:
1 <= T<= 100
1 <= n <= 103
1 <= arr[i] <= 103

Example:
Input:
3
5
40 30 35 80 100
8
40 30 32 35 80 90 100 120
8
7  9 6 1 4 2 3 40

Output:
1
1
0

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Preorder Traversal and BST

Output Window