#include<iostream>
#include<climits>
using namespace std;
int main(){
int n;
cin>>n;
int a[100];
int left_max[100];
int maax=INT_MIN;
for (int i = 0; i < n; i++)
{
cin>>a[i];
maax=max(a[i],maax);
left_max[i]=maax;
}
int right_max[100];
maax=INT_MIN;
for ( int i = n-1; i>=0; i--)
{
maax=max(a[i],maax);
right_max[i]=maax;
}
int water_saved=0;
for (int i = 0; i < n; i++)
{
int mini=min(left_max[i],right_max[i]);
water_saved+=a[i]-mini;
}
cout<<abs(water_saved);
return 0;
}
On incresing the size of arrays the code is giving segementation fault core dumped.A/c to constraints the size of arra must me 1000000
@sagar_aggarwal hey make these changes in your code
int n;
cin>>n;
int a[n];
int left_max[n];
int right_max[n];
after submitting value of n as 1000000 program is getting closed by itself after applying your changes. How can i declare arrays globally?
Can you please edit my code and share it with me