Range Minimum Query | Practice | GeeksforGeeks

Given an array A[ ] and its size N your task is to complete two functions  a constructST  function which builds the segment tree  and a function RMQ which finds range minimum query in a range [a,b] of the given array .

Input:
The task is to complete two functions constructST and RMQ .
The constructST function builds the segment tree and takes two arguments the array A[ ] and the size of the array N .
It returns a pointer to the first element of the segment tree array . 
The RMQ function takes 4 arguments the first being the segment tree st constructed, second being the size N and then third and forth arguments are the range of query a and b .The function RMQ returns the min of the elements in the array from index range a and b. There are multiple test cases. For each test case, this method will be called individually.

Output:
The function RMQ should return the min element in the array from range a to b .

Constraints:
1<=T<=100
1<=N<=10^3+1
1<=Q(no of queries)<=10000
0<=a<=b
Example:
Input (To be used only for expected output) 
1
4
1 2 3 4
2
0 2 2 3

Output
1 3

Explanation
1. For query 1 ie 0 2 the element in this range are 1 2 3 and the min element is 1  
2. For query 2 ie 2 3 the element in this range are 3 4 and the min element is 3 
0
punitkumarojha4 months ago
Since there are no Update Queries we can solve the problem in 
O(nlogn) - for constructing the sparse table
O(1) - for answering each query
 
int st[2],dp[1010][13];

int *constructST(int a[],int n){
    for(int i=0;i<n;i++)
        dp[i][0]=a[i];
    for(int j=1;j<=12;j++)
        for(int i=0;(i+(1<<j))<=n;i++)
            dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
    return st;
}

int RMQ(int st[], int n,int a,int b){
    int j,lg[14];
    lg[1]=0;
    for(int i=2;i<14;i++)
        lg[i]=1+lg[i>>1];
    j=lg[abs(b-a)+1];
    return min(dp[a][j],dp[b-(1<<j)+1][j]);
}
0
Anil Maurya7 months ago
Anil Maurya
Correct Code 🥳🥳C++https://ide.geeksforgeeks.org/6mu6qgKs0g
 
 
 
0
aditya anand1 year ago
aditya anand
python please
0
hansal kothari1 year ago
hansal kothari
https://ide.geeksforgeeks.o...Exec time : 0.03 // dont miss to give segment tree size above 4*n , otherwise you will get segmentation fault.
0
BITAN SARKAR2 years ago
BITAN SARKAR
Java Solution...public static void fill(int l, int r, int[] arr, int p){        if(l>r)return;        if(l==r){            st[p]=arr[l];            return;        }        int m = l+(r-l)/2;        fill(l,m,arr,2*p+1);        fill(m+1,r,arr,2*p+2);        st[p]=Math.min(st[2*p+1], st[2*p+2]);    }    public static int[] constructST(int arr[], int n){        int nc = n;        int len = 0;        while(nc!=0){            len++;            nc/=2;         }        len=(int)Math.pow(2,len+1);        st = new int[len];        for(int i=0;i<len;i++)st[i]=integer.max_value; fill(0,n-1,arr,0);="" for(int="" i="0;i&lt;len;i++)System.out.print(st[i]+"\t");" system.out.println();="" return="" st;="" add="" your="" code="" here="" }="" public="" static="" int="" findmin(int="" st[],="" int="" l,="" int="" r,="" int="" l,="" int="" r,="" int="" p){="" int="" m="L+(R-L)/2;" if(l="=L" &&="" r="=R)return" st[p];="" if(l="=L" &&="" r="=M)return" st[2*p+1];="" if(l="=M+1" &&="" r="=R)return" st[2*p+2];="" if(l="">=L && r<=M)return findMin(st,L,M,l,r,2*p+1);        if(l>M && r<=R)return findMin(st,M+1,R,l,r,2*p+2);        int p1 = findMin(st,L,M,l,M,2*p+1);        int p2 = findMin(st,M+1,R,M+1,r,2*p+2);        return Math.min(p1,p2);     }    /* The functions returns the      min element in the range      from l and r */    public static int RMQ(int st[], int n, int l, int r){        return findMin(st,0,n-1,l,r,0);       // Add your code here    }
0
G Vishal2 years ago
G Vishal
java solution https://ide.geeksforgeeks.o...https://uploads.disquscdn.c...
0
Deepak Kumar2 years ago
Deepak Kumar
@admin please add python
0
sanyam2 years ago
sanyam
https://ide.geeksforgeeks.o... https://uploads.disquscdn.c...
0
sanyam2 years ago
sanyam
void  build_tree(int *tree,int *arr,int index,int s,int e){  if(s>e){      return ;  }    if(s==e)  {      tree[index]=arr[s];      return;  }  // recursive case  int mid=(s+e)/2;  build_tree(tree,arr,2*index,s,mid);  build_tree(tree,arr,2*index+1,mid+1,e);  int left=tree[2*index];  int right=tree[2*index+1];  tree[index]=  min(left,right);  return ;}
int *constructST(int arr[],int n){
int *tree=new int[4*n+1];build_tree(tree,arr,1,0,n-1);return tree;
}
/* The functions returns the min element in the range from a and b */
int query(int *tree,int index,int s,int e,int qs,int qe){
   // no overlap case;    if(  qs>e||qe=e){        return tree[index];    }
    // partial overlap case;    int mid=(s+e)/2;    int left=query(tree,2*index,s,mid,qs,qe);    int right=query(tree,2*index+1,mid+1,e,qs,qe);    return min(left,right);}
int RMQ(int *tree, int n, int a, int b){ return query(tree,1,0,n-1,a,b);}
 https://uploads.disquscdn.c...
0
RAJ PATEL2 years ago
RAJ PATEL
Faster iterative sol which requires exactly 2*n memoryhttps://ide.geeksforgeeks.o...
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.