Check given array of size n can represent BST of n levels or not | C ++ program to Check given array can represent BST 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int arr [ ] = { 5123 , 3300 , 783 , 1111 , 890 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int max = INT_MAX ; int min = INT_MIN ; bool flag = true ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > min && arr [ i ] < max ) { min = arr [ i - 1 ] ; } else if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] > min && arr [ i ] < max ) { max = arr [ i - 1 ] ; } else { flag = false ; break ; } } if ( flag ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }