forked from Shreyasheeetal20/HACKTOBERFEST22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum Area Histogram.cpp
87 lines (86 loc) · 1.9 KB
/
Maximum Area Histogram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout<<"Maximum Area Histogram"<<endl;
cout<<"Enter number of elements: ";
int n;
cin>>n;
cout<<"Enter elements:"<<endl;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
stack<pair<int,int>> stk1;
stack<pair<int,int>> stk2;
pair<int,int> p;
vector<int> left;
vector<int> right;
for(int i=0;i<n;i++)
{
if(stk1.empty())
{
left.push_back(-1);
}
else
{
while(stk1.empty()==false && stk1.top().first>=arr[i])
{
stk1.pop();
}
if(stk1.empty()) left.push_back(-1);
else left.push_back(stk1.top().second);
}
p.first=arr[i];
p.second=i;
stk1.push(p);
}
for(int i=n-1;i>=0;i--)
{
if(stk2.empty())
{
right.push_back(n);
}
else
{
while(stk2.empty()==false && stk2.top().first>=arr[i])
{
stk2.pop();
}
if(stk2.empty()) right.push_back(n);
else right.push_back(stk2.top().second);
}
p.first=arr[i];
p.second=i;
stk2.push(p);
}
reverse(right.begin(),right.end());
vector<int> width;
for(int i=0;i<n;i++)
{
width.push_back(right[i]-left[i]-1);
}
int max=INT_MIN;
for(int i=0;i<n;i++)
{
if(arr[i]*width[i]>max) max=arr[i]*width[i];
}
cout<<"\nElements in array: ";
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<"\nNearest Smallest to Left array (Indices): ";
for(int i=0;i<n;i++)
{
cout<<left[i]<<" ";
}
cout<<"\nNearest Smallest to Right array (Indices): ";
for(int i=0;i<n;i++)
{
cout<<right[i]<<" ";
}
cout<<"\nMax area of histogram: "<<max;
return 0;
}