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.
classSolution { public: //Function to find largest rectangular area possible in a given histogram. longlonggetMaxArea(longlong arr[], int n){ vector<pair<longlong, longlong>> st; longlong res = 0; for(int i = 0; i < n; i++) { int left = i; while(!st.empty() and st.back().second >= arr[i]) { res = max(res, (i - st.back().first) * st.back().second); left = st.back().first; st.pop_back(); } st.push_back({left, arr[i]}); } for(int i = 0; i < st.size(); i++) { res = max(res, (n - st[i].first) * st[i].second); } return res; } };