From 28a16465d52a10982a76f7d5966cc168ab0a1d40 Mon Sep 17 00:00:00 2001 From: MdIfham <76588472+MdIfham@users.noreply.github.com> Date: Wed, 26 Oct 2022 23:50:55 +0530 Subject: [PATCH] Demo for Histogram Area --- maxAreaInHistogram.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 maxAreaInHistogram.cpp diff --git a/maxAreaInHistogram.cpp b/maxAreaInHistogram.cpp new file mode 100644 index 00000000..7c7a2728 --- /dev/null +++ b/maxAreaInHistogram.cpp @@ -0,0 +1,45 @@ +#include +using namespace std; + + +//Function to find largest rectangular area possible in a given histogram. +long long getMaxArea(long long arr[], int n) +{ + // Your code here + stack s; + long long int res = 0; + long long int tp; + long long int curr; + + for (int i = 0;i < n;i++) { + while (s.empty() == false && arr[s.top()] >= arr[i]) { + tp = s.top();s.pop(); + curr = arr[tp] * (s.empty() ? i : i - s.top() - 1); + res = max(res, curr); + } + s.push(i); + } + while (s.empty() == false) { + tp = s.top();s.pop(); + curr = arr[tp] * (s.empty() ? n : n - s.top() - 1); + res = max(res, curr); + } + + return res; +} + + + +int main() { + system("cls"); + long long n; + cin >> n; + long long arr[n]; + for (long long i = 0; i < n; i++) { + cin >> arr[i]; + } + + cout << getMaxArea(arr, n) << "\n"; + + return 0; +} \ No newline at end of file