-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_80.cpp
More file actions
111 lines (86 loc) · 2.13 KB
/
Day_80.cpp
File metadata and controls
111 lines (86 loc) · 2.13 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
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.
Input:
N = 8
arr[] = {7 2 8 9 1 3 6 5}
Output: 16
Explanation: Maximum size of the histogram
will be 8 and there will be 2 consecutive
histogram. And hence the area of the
histogram will be 8x2 = 16.
*/
// **********************************************************************************************************
# include <bits/stdc++.h>
using namespace std;
int max_area_hist(int arr[], int c)
{
stack <int> stk;
int left[c];
int right[c];
for (int i = 0; i < c; i++)
{
while(true)
{
if(stk.empty())
{
stk.push(i);
left[i] = 0;
break;
}
int temp = stk.top();
if (arr[i] <= arr[temp]) {
stk.pop();
}
else {
left[i] = temp + 1;
stk.push(i);
break;
}
}
}
stack <int> st;
for (int i = c-1; i >= 0; i--)
{
while(true)
{
if(st.empty())
{
st.push(i);
right[i] = c-1;
break;
}
int temp = st.top();
if (arr[i] <= arr[temp]) {
st.pop();
}
else {
right[i] = temp - 1;
st.push(i);
break;
}
}
}
int area = 0;
for (int i = 0; i < c; i++)
{
int each_area = (right[i] - left[i] + 1) * arr[i];
if (each_area > area) area = each_area;
}
return area;
}
int main()
{
int n;
cout<<"Enter length of historam: ";
cin>>n;
int arr[n];
cout<<"Enter elements of array: ";
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
int max_area = max_area_hist(arr, n);
cout<<"Max area is: "<<max_area<<endl;
return 0;
}