I am not getting why is there a segmentation fault occurring. Kindly help me resolve it. If I am changing the size of array to some lesser value it is executing. Is there any limit on array size but the question demands that size equal to 1000000. What should I do apart from using vector.
Link to question: https://practice.geeksforgeeks.org/problems/maximum-rectangular-area-in-a-histogram-1587115620/1#
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
//Function to find largest rectangular area possible in a given histogram.
long long arrN[1000000]={0};
long long arrP[1000000]={0};
void prevS(long long arr1[],int n)
{ stack<long long> s;
for(int i=0;i<n;i++)
{
if(s.empty()){s.push(i); arrP[i]=-1;}
else
{
while(!s.empty() && arr1[s.top()]>=arr1[i])
{
s.pop();
}
if(s.empty()) arrP[i]=-1;
else
arrP[i]=s.top();
s.push(i);
}
}
}
void nexS(long long arr1[],int n)
{ stack<long long> s;
for(int i=n-1;i>=0;i--)
{
if(s.empty()){s.push(i); arrN[i]=n;}
else
{
while(!s.empty() && arr1[s.top()]>=arr1[i])
{
s.pop();
}
if(s.empty()) arrN[i]=n;
else
arrN[i]=s.top();
s.push(i);
}
}
}
long long getMaxArea(long long arr[], int n)
{
// Your code here
prevS(arr,n);
long long* pre=arrP;
nexS(arr,n);
long long* nex=arrN;
long long ans=INT_MIN;
for(int i=0;i<n;i++)
{
ans=max(ans,arr[i]*(nex[i]-pre[i]-1));
}
return ans;
}
};
int main()
{
long long t;
cin>>t;
while(t--)
{
int n;
cin>>n;
long long arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
Solution ob;
court<<ob.getMaxArea(arr,n)<<endl;
}
return 0;
}
Input:
1
7
6 2 5 4 5 1 6