fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int largestHistogram(int arr[] , int n){
  4.  
  5. stack<int> s;
  6. int max_area=0;
  7. int area=0;
  8. int i=0;
  9. while(i<n){
  10.  
  11. if(s.empty() || arr[s.top()]<=arr[i]){
  12. //pushing the index
  13. s.push(i);
  14. i++;
  15. }
  16. else{
  17.  
  18. //comuting the area
  19.  
  20.  
  21. int curr_max =s.top();
  22. s.pop();
  23. int area = arr[curr_max] * (s.empty()?i:(i-s.top()-1) );
  24. if(area>max_area)
  25. max_area=area;
  26. }
  27. }
  28.  
  29. while(!s.empty()){
  30. int curr_max =s.top();
  31. s.pop();
  32.  
  33. int area = arr[curr_max] * (s.empty()?i:(i-s.top()-1) );
  34. if(area>max_area)
  35. max_area=area;
  36. }
  37.  
  38. return max_area;
  39. }
  40.  
  41. int main() {
  42.  
  43. int n =0;
  44. cin>>n;
  45. int arr[n];
  46. for(int i =0;i<n;i++)
  47. cin>>arr[i];
  48.  
  49. cout<<largestHistogram(arr , n);
  50. return 0;
  51. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty