Histogram area problem

my code is giving segmentation error when i try to submit it on gfg


please help

Hey @gulatigarvita08
Please share the link where u are submitting it :slight_smile:

https://practice.geeksforgeeks.org/problems/maximum-rectangular-area-in-a-histogram-1587115620/1#

Hey @gulatigarvita08
I think u shared the incomplete code , because long getMaxArea(long long arr[], int n) function is missing in it,please reshare the complete code :slight_smile:

oh sorry :sweat_smile:

Hey @gulatigarvita08
Mentioned the changes in comments : https://ide.codingblocks.com/s/370880 :slight_smile:

You are getting segmentation because of static allocation
Max size arrays u can create inside function statically is close to 10^6 so sometimes it gives segmentation

but 1<=n<=10^6
so why long long

n is array size but array elements can be of order 10^18 that is why long long

that array i took as long long
but lb and rb are just storing idexes … so ig it can have static allocation

Oh yes ,sorry u can use int for them :sweat_smile:

but then why my code is giving segmentation error :sweat_smile:

Irrespective of int array or long long array

sorry could’nt understand :sweat_smile:
when do we statically allocate ??

int arr[n] // this is static allocation

int*arr=new int[n] //this is dynamic allocation

i mean why are we dynamically allocating memory to it

I just told u that maximum size arrays u can create inside any function is 10^6 when allocating them statically because of limited stack memory

So sometimes this gives segmentation error

To overcome this we allocate space dyanmically i.e from heap .

but still the thing is we have 1<=n<=10^6 … so why dynamically

assume n is 10^6
And u declare two arrays
int arr1[1000000]
int arr2[1000000] but here memory in stack was not available hence it will give segmentation error. thats why dynamically

code is giving runtime error …

Please share the code again :slight_smile: