What is wrong in my code of Largest Area under Histogram using Stack and please send the code

class Solution
{
public:
//Function to find largest rectangular area possible in a given histogram.
long long getMaxArea(long long arr[], int n)
{

    stack<int> s;
    
    int max_area=0;
    
    int i=0;
  while(i<n)
  {  
       while(!s.empty() and arr[i]<arr[s.top()])
            {
                int t=s.top();
                int h=arr[t];
                s.pop();
                
                if(s.empty())
                {
                   max_area=max(max_area,h*i);   
                }
                else
                {
                max_area=max(max_area,h*(i-t-1));
               }
        }
        
        s.push(i);   //It also handles the insertion of the first element
        i++;

    }
    

    return max_area;    
}   

};

hello @Subrat
u need to consider case where while loop has ended but stack is still not empty.

also pls save ur code at cb ide and share its url with me

Sir please improve this code.

check last while loop of this code. ->

that is added to handle the case when first while loop ended. but stack is still not empty