Inorder Traversal and BST | Practice | GeeksforGeeks

Given an array arr of size N, write a program that returns 1 if array represents Inorder traversal of a BST, else returns 0.
Note: All keys in BST must be unique.

Example 1:
Input:
N = 3
arr = {2, 4, 5}
Output: 1
Explaination: Given arr representing
inorder traversal of a BST.


Example 2:
Input:
N = 3
arr = {2, 4, 1}
Output: 0
Explaination: Given arr is not representing
inorder traversal of a BST.

Your Task:
You don't need to read input or print anything. Your task is to complete the function isRepresentingBST() which takes the array arr[] and its size N as input parameters and returns 1 if array represents Inorder traversal of a BST, else returns 0. 

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 103
1 ≤ arr[i] ≤ 103
0
vrajeshmodi991 month ago
Recursive approach:
int isRepresentingBST(int arr[], int N)
    {
       if(N==0)return 1;
       
        if(arr[N-1]<=arr[N-2])return 0;
        
        return isRepresentingBST(arr,N-1);
    }
0
vrajeshmodi991 month ago
 int isRepresentingBST(int arr[], int N)
    {
        for(int i=0;i<N-1;i++){
            if(arr[i]>=arr[i+1]){
                return 0;
            }
        }
        return 1;
    }
0
guptaram4656912 months ago
     {
        set<int> s;       for (int i = 0; i < N - 1; i++)       {           s.insert(arr[i]);           if (arr[i] > arr[i + 1])           {               return 0;           }       }       s.insert(arr[N - 1]);       if (s.size() != N)       {           return 0;       }       return 1;
 
}
0
detroix072 months ago
int isRepresentingBST(int arr[], int n)   {     for(int i=1;i<n;i++){      if(arr[i] <=  arr[i-1]) return 0;     }     return 1;    }
0
harshit68rocking2 months ago
 int isRepresentingBST(int arr[], int N)
    {
        // code here
        int c = arr[0];
        for(int i =1;i<N;i++){
            if(arr[i] <= c) return 0;
            else c= arr[i];
        }
        return 1;
    }
0
singhsrijan8093 months ago
JAVA SOLUTION 0.3/5.3 sec
PLAYERG SOLUTIONS
 
static int isRepresentingBST(int a[], int n)
   {
       // code here
       if(n==1) return 1;
       
       for(int i=1;i<n;i++){
           if(a[i-1]>=a[i]) return 0;
       }
       
       
       return 1;
   }
   LIVE GREEN PLAYERG..!!!!!!!!!PGxJAANWAR
0
kronizerdeltac3 months ago
C++ Solution - 0 ms passed.
int isRepresentingBST(int arr[], int N)   {       if(N == 1)           return 1;                  for(int i = 1; i < N; i++) {           int prev = arr[i - 1], curr = arr[i];           if(prev >= curr)               return 0;       }       return 1;   }
0
chessnoobdj3 months ago
C++ one liner STL
int isRepresentingBST(int arr[], int N)
    {
        return is_sorted(arr, arr+N, [](int a, int b){
            return a<=b;
        });
    }
+1
badgujarsachin836 months ago
int isRepresentingBST(int arr[], int N)
    {
        // code here
        for(int i=0;i<N-1;i++){
            if(arr[i]>=arr[i+1]){
                return 0;
            }
        }
        return 1;
    }
-1
vs5094117 months ago
not accepting the correct solution 
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.