Maximum Rectangular Area in a Histogram | Practice | GeeksforGeeks

Find the largest rectangular area possible in a given histogram where the largest rectangle can be made of a number of contiguous bars. For simplicity, assume that all bars have the same width and the width is 1 unit, there will be N bars height of each bar will be given by the array arr.
Example 1:
Input:
N = 7
arr[] = {6,2,5,4,5,1,6}
Output: 12
Explanation: 



Example 2:
Input:
N = 8
arr[] = {7 2 8 9 1 3 6 5}
Output: 16
Explanation: Maximum size of the histogram 
will be 8  and there will be 2 consecutive 
histogram. And hence the area of the 
histogram will be 8x2 = 16.
Your Task:
The task is to complete the function getMaxArea() which takes the array arr[] and its size N as inputs and finds the largest rectangular area possible and returns the answer.
Expected Time Complxity : O(N)
Expected Auxilliary Space : O(N)
Constraints:
1 ≤ N ≤ 106
1 ≤ arr[i] ≤ 1012
0
coder_dhanush51 minutes ago
easy cpp solution u tube anuj bhaiya 
  vector<long long> lst(long long arr[],int n)   {       stack<int>s;       vector<long long>v;       for(int i=0;i<n;i++)       {           while(!s.empty() && arr[s.top()] >= arr[i])           {               s.pop();           }           if(s.empty())           {               v.push_back(-1);           }           else{               v.push_back(s.top());           }           s.push(i);       }       return v;   }       vector<long long> rst(long long arr[],int n)   {       stack<int>s;       vector<long long>v;       for(int i=n-1;i>=0;i--)       {           while(!s.empty() && arr[s.top()] >= arr[i])           {               s.pop();           }           if(s.empty())           {               v.push_back(n);           }           else{               v.push_back(s.top());           }           s.push(i);       }       reverse(v.begin(),v.end());       return v;   }      long long getMaxArea(long long arr[], int n)   {       long long mxsum=0;       vector<long long> ls= lst(arr,n);             vector<long long> rs=rst(arr,n);       for(int i=0;i<n;i++)       {           long long curr=(rs[i]-ls[i]-1)*arr[i];           mxsum=max(curr,mxsum);       }       return mxsum;   }}; 
video se smjh jaoge simply
0
abhishekvicky123452 days ago
/*Java Solution*/
class Solution{   public static long getMaxArea(long nums[], long n)    {        Stack<Integer> hs=new Stack<>(); long[] left=new long[nums.length];        for(int i=0;i<nums.length;i++)       {        while(!hs.empty() && nums[hs.peek()]>=nums[i])         hs.pop();        if(hs.empty())left[i]=0;        else left[i]=hs.peek()+1;        hs.push(i);                }            while(!hs.empty())hs.pop();    long[] right=new long[nums.length];        for(int i=nums.length-1;i>=0;i--)       {        while(!hs.empty() && nums[hs.peek()]>=nums[i])         hs.pop();        if(hs.empty())right[i]=nums.length-1;        else right[i]=hs.peek()-1;        hs.push(i);                }        long max=-10000000000L;       for(int i=0;i<nums.length;i++)       {        max=Math.max(max,(right[i]-left[i]+1)*nums[i]);       }       return max;   }       }
 
 
 
 
0
debayanbiswas313 days ago
long long getMaxArea(long long arr[], int n)
    {
        // Your code here
        stack<int>st;
        vector<int>left(n);
        vector<int>right(n);
        
        
        for(int i=0;i<n;i++)
        {
            while(st.size()!=0 && arr[i]<=arr[st.top()])
            {
                st.pop();
            }
            left[i]= st.size()!=0?st.top()+1:0;
            st.push(i);
            
        }
        
        while(st.size()!=0) st.pop();
        
        for(int i=n-1;i>=0;i--)
        {
            while(st.size()!=0 && arr[i]<=arr[st.top()])
            {
                st.pop();
            }
            
            right[i]=st.size()!=0?st.top()-1:n-1;
            st.push(i);
        }
        long long mx=0;
        for(int i=0;i<n;i++)
        {
            
            long long curr_area=(right[i]-left[i]+1)*arr[i];
            
            mx=max(mx,curr_area);
        }
        return mx;
        
    }
0
akkeshri140420011 week ago
long long getMaxArea(long long arr[], int n)
   {
       // Your code here
       vector<long long>left(n),right(n);
       stack<long long>stk;
       for(int i=0;i<n;i++)
       {
           while(!stk.empty() and arr[i]<=arr[stk.top()]){
               stk.pop();
           }
           if(stk.empty()){
               left[i]=0;
           }
           else{
               left[i]=stk.top()+1;
           }
           stk.push(i);
       }
       while(!stk.empty()){
           stk.pop();
       }
       for(int i=n-1;i>=0;i--){
           while(!stk.empty() and arr[i]<=arr[stk.top()]){
               stk.pop();
           }
           if(stk.empty()){
               right[i]=n-1;
           }
           else{
               right[i]=stk.top()-1;
           }
           stk.push(i);
       }
       long long maxi=0;
       for(int i=0;i<n;i++){
           maxi=max(maxi,(right[i]-left[i]+1)*arr[i]);
       }
       return maxi;
       
   }
0
ujjwalraj8220011 week ago
JAVA Solution using Stack 
 
class Solution{   //Function to find largest rectangular area possible in a given histogram.   public static long getMaxArea(long hist[], long n)    {       // your code here       int size = hist.length;       long[] left = nsl(hist, size);       long[] right = nsr(hist, size);       long[] width = new long[size];       long[] area = new long[size];       for(int i = 0; i<size; i++) {           width[i] = right[i] - left[i] -1;           area[i] = (width[i] * hist[i]);       }       long max = area[0];        for(int i = 1; i<size; i++) {           if(area[i] > max) {               max = area[i];           }       }              return max;   }      //Function to find nearest smaller element to left   public static long[] nsl(long[] hist, int n) {       long[] index = new long[n];       Stack<long[]> s = new Stack<>();       for(int i = 0; i<n; i++) {           if(s.isEmpty()) {               index[i] = -1;           }           else if(!s.isEmpty() && s.peek()[0] >= hist[i]) {               while(!s.isEmpty() && s.peek()[0] >= hist[i]) {                   s.pop();               }               if(s.isEmpty()) {                   index[i] = -1;               }               else{                   index[i] = s.peek()[1];               }           }           else{               index[i] = s.peek()[1];           }           s.push(new long[] {hist[i], i});       }       return index;   }      //Function to find nearest smaller element to right   public static long[] nsr(long[] hist, int n) {       long[] index = new long[n];       Stack<long[]> s = new Stack<>();       for(int i = n-1; i>=0; i--) {           if(s.isEmpty()) {               index[i] = n;           }           else if(s.peek()[0] >= hist[i]) {               while(!s.isEmpty() && s.peek()[0]>= hist[i]) {                   s.pop();               }               if(s.isEmpty()) {                   index[i] = n;               }               else {                   index[i] = s.peek()[1];               }           }           else {               index[i] = s.peek()[1];           }           s.push(new long[] {hist[i], i});       }       return index;   }}
 
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.