My code successfully got submitted on this platform but the same code was getting Segmentation Fault (SIGSEGV) on GeeksForGeeks platform.
Can you please identify what’s wrong in this code and why I am getting segmentation fault?
Here is my code:
include <bits/stdc++.h >
using namespace std;
long int kadane(long int a[],long int n)
{
long int ms=INT_MIN;
long int cs=0;
for(int i=0;i<n;i++)
{
cs=cs+a[i];
if(ms<cs)
{
ms=cs;
}
if(cs<0)
{
cs=0;
}
}
return ms;
}
int main()
{
int t;
cin>>t;
while(t--){
long int n;
cin>>n;
long int a[1001];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
long int ms,cs=0;
ms=kadane(a,n);
//cout<<ms<<"\n";
long int y;
for(long int i=0;i<n;i++)
{
cs=cs+a[i];
a[i]=-a[i];
}
//cout<<cs<<"\n";
y=kadane(a,n);
cs=cs+y;
long int ans=max(cs,ms);
if(ans==0)
{
ans=min(cs,ms);
}
cout<<ans<<"\n";
}
}