Largest histogram area

code running fine with custom inputs but not passing test cases

hi @rabimajumder08
try this–>

#include<iostream>
#include<stack>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
	int n;
	cin>>n;
	vector<long long int> heights;
	for(int i=0;i<n;i++){
		int no;
		cin>>no;
		heights.push_back(no);
	}

        long long int nsr[n];
        stack<long long int> sr;
        nsr[n-1] = n;
        sr.push(n-1);
        for(int i=n-2;i>=0;i--){
            while(sr.size()>0 && heights[sr.top()]>= heights[i]){
                sr.pop();
            }
            
            if(sr.size() == 0){
                nsr[i] = n;
            }
            else{
                nsr[i] = sr.top();
            }
            sr.push(i);
        }
        
        //left boundry
        long long int nsl[n];
        stack<long long int> sl;
        nsl[0] = -1;
        sl.push(0);
        for(int i=1;i<n;i++){
            while(sl.size()>0 && heights[sl.top()]>= heights[i]){
                sl.pop();
            }
            
            if(sl.size() == 0){
                nsl[i] = -1;
            }
            else{
                nsl[i] = sl.top();
            }
            sl.push(i);
        }
        
        long long int maxarea = 0;
        for(int i=0;i<n;i++){
            int width = nsr[i] - nsl[i] - 1;
            long long int area = heights[i]*width;
            maxarea = max(maxarea, area);
        }

		cout<<maxarea<<endl;

	return 0;
}

change int to long long int…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.