
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Largest Rectangular Area in a Histogram using C++
A histogram is a visual representation of the distribution of quantitively data. You are given a histogram represented by an array arr [], Where each of the array's elements represents the height of the bar in the histogram. All the bars have unit width, i.e., 1 unit.
Your task is to determine the largest rectangle area possible in a given histogram, where the largest rectangle is made up of several consecutive bars with heights displayed in an array.
Input / Output Scenario
Let's understand the below input-output scenario with a diagram:
Input: arr[] = [40, 10, 20, 50, 60, 30] Output: 100![]()
Explanation: Since we get the maximum area by picking bars [50, 60]. So the area is calculated using the (smallest bar from the picked bars) * (Number of the picked bars).
Output is: 50*2 = 100
This is another input/output scenario:
Input: arr[] = [30, 20, 40, 50, 70] Output: 120Explanation: Since we get the maximum area by picking bars [40, 50, 60]. So the area is calculated using the (smallest bar from the picked bars) * (Number of the picked bars).
Output is: 40*3 = 120
Find Largest Area of rectangle in a Histogram Using Naive Approach
In this method (Naive), you will consider each bar as the smallest height and calculate the largest area. You travel to the left of it and add its height until you reach a smaller element. You follow the same steps for the right side.
The area of the current bar as a minimum is going to be the height or current bar multiplied by the total width traversed on both left and right including the current bar. The area is the smallest selected bar's height multiplied by the total number of bars.
Example
C++ program to find the largest rectangular area possible in a given histogram:
#include <bits/stdc++.h> using namespace std; int max_area(vector < int > & arr) { int res = 0, n = arr.size(); // Consider every bar one by one for (int i = 0; i < n; i++) { int curr = arr[i]; // Traverse left for (int j = i - 1; j >= 0 && arr[j] >= arr[i]; j--) curr += arr[i]; // Traverse right for (int j = i + 1; j < n && arr[j] >= arr[i]; j++) curr += arr[i]; res = max(res, curr); } return res; } int main() { vector < int > arr = { 40, 10, 20, 50, 60, 30 }; cout << "Largest Rectangle Ares:" << max_area(arr); return 0; }
Following is the output of the above code:
Largest Rectangle Ares:100
Find Largest Area of rectangle in a Histogram Using Single stack
This approach is an optimized approach over the previous approach. Which takes O(n) time and O(n) Space complexity. Below is the step-by-step implementation: stack Data structure
- First Create an empty stack.
- Start with the first bar and proceed with each bar in arr[i], where i ranges from 0 to n-1.
- If the stack is empty or arr[i] is greater than the bar at the top, push i to the stack.
- If this bar is smaller than the top of the stack, proceed to remove the top of the stack while it is greater.
- Let removed bar be arr[tp]. Calculate the area of the rectangle with arr[tp] as the smallest bar.
- The 'left index' of arr[tp] is the previous (previous to tp) item in the stack, whereas the 'right index' is 'i' (current index).
- If the stack is not empty, then one by one remove all bars from the stack and do steps (2.2 and 2.3) for every removed bar.
Example
C++ program to find the largest rectangular area possible in a given histogram using the stack:
#include <bits/stdc++.h> using namespace std; int max_area(vector < int > & arr) { int n = arr.size(); stack < int > s; int res = 0; int tp, curr; for (int i = 0; i < n; i++) { while (!s.empty() && arr[s.top()] >= arr[i]) { // smallest element of the Histogram tp = s.top(); s.pop(); int width = s.empty() ? i : i - s.top() - 1; res = max(res, arr[tp] * width); } s.push(i); } while (!s.empty()) { tp = s.top(); s.pop(); curr = arr[tp] * (s.empty() ? n : n - s.top() - 1); res = max(res, curr); } return res; } int main() { vector < int > arr = {30, 20, 40, 50, 70}; cout << "Largest Rectangle Ares:" << max_area(arr); return 0; }
Following is the output:
Largest Rectangle Ares:120