Check given array of size n can represent BST of n levels or not | Driver Code ; This element can be inserted to the right of the previous element , only if it is greater than the previous element and in the range . ; max remains same , update min ; This element can be inserted to the left of the previous element , only if it is lesser than the previous element and in the range . ; min remains same , update max ; if the loop completed successfully without encountering else condition
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5123 , 3300 , 783 , 1111 , 890 ] NEW_LINE n = len ( arr ) NEW_LINE max = 2147483647 NEW_LINE min = - 2147483648 NEW_LINE flag = True NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] and arr [ i ] > min and arr [ i ] < max ) : NEW_LINE INDENT min = arr [ i - 1 ] NEW_LINE DEDENT elif ( arr [ i ] < arr [ i - 1 ] and arr [ i ] > min and arr [ i ] < max ) : NEW_LINE INDENT max = arr [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT