the code is working fine on boyh sublime text 3 and coding blocks ide but on submission it shows ‘run error’
Maximum subarray
Please share your code.
#include
using namespace std;
int main(int argc, char const *argv[])
{
int t,n;
cin>>t;
while(t>0)
{
cin>>n;
int ms=0;
int cur=0;
signed int a[10000];
signed int cs[10000];
cin>>a[0];
cs[0]=a[0];
for (int i = 1; i < n; i++)
{
cin>>a[i];
cs[i]=cs[i-1]+a[i];
}
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
cur=cs[j]-cs[i-1];
if (cur>ms)
{
ms=cur;
}
}
}
cout<<ms<<endl;
/*
//kadane's algorithm
signed int cs=0;
signed int ms=0;
for (int i = 0; i < n; i++)
{
cs=cs+a[i];
if (cs<0)
{
cs=0;
}
ms= max(cs,ms);
}
cout<<ms<<endl;
*/
t--;
}
return 0;
}
Why did you comment the kadane’s part?
Also it would show run error because you are trying to access cs[-1] when i is equal to 0.