In this question, my code is working correctly for the given test case, but still, it is not getting submitted.
Also, please guide if one of the inputs provided is a negative integer.
In this question, my code is working correctly for the given test case, but still, it is not getting submitted.
Also, please guide if one of the inputs provided is a negative integer.
There won’t be any negative element present in the test case cause it’s mentioned
Height of each bar in histogram <= 10^9
So take all element as long long int
And i saw your code, it’s not correctly implemented cause for the test case:
7
2 3 1 4 6 8 1
Expected output is: 12
Yours is giving: 7
Though your logic is correct it’s just implementation issue. Try to deal with it, if you fail let me know i will help you.
Hi,
I modified my code accordingly and now it is giving the correct answer for the test case provided by you, but still, it is not getting submitted.
Here is the code:
#include
#include
using namespace std;
int areahist(int hist[], int n)
{
stack s;
int max_area = 0;
int tp;
int area_with_top;
int i = 0;
while (i < n)
{
if (s.empty() || hist[s.top()] <= hist[i])
{
s.push(i++);
}
else
{
tp = s.top();
s.pop();
area_with_top = hist[tp] * (s.empty() ? i : i - s.top() - 1);
if (max_area < area_with_top)
{
max_area = area_with_top;
}
}
}
while (s.empty() == false)
{
tp = s.top();
s.pop();
area_with_top = hist[tp] * (s.empty() ? i : i - s.top() - 1);
if (max_area < area_with_top)
{
max_area = area_with_top;
}
}
return max_area;
}
int main()
{
int n;
cin>>n;
int hist[n];
for(int i=0;i<n;i++)
{
cin>>hist[i];
}
cout << areahist(hist, n);
return 0;
}
Can you share your code using ide.codingblocks.com so that it would be easy for me to debug and tell you your mistake.
Actually, I’m quite new to coding blocks, so I don’t know how to do that
It’s fine i have already done that, debugging and correcting your code just give me few minutes.
Hey, i have debugged your code here and also have added few comments for better understanding of problem statements with various test cases. You can see this code, it’s getting accepted with 100% result too.
Thank you so much, sir, for your help
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
just do 2 things, do dry run as much as you can for this code else you will forget it for sure.
Secondly don’t forget to rate your experience with me.