Maximum subaarray sum

I AM HAVING WRONG ANSWER FOR ALL TEST CASES AS MY OUTPUT AFTER SUBMITTING THIS CODE.
IS THIS CODE WRONG?? (IOSTREAM IS INCLUDED HERE BUT IT IS NOT SHOWING HERE IN THIS POST SO THE ERROR IS NOT DUE TO IT)
#include
using namespace std;

long long int maxSubArraySum(long long int a[],long long int s)
{
long long int ms=0,cs=0;
for (long long int i=0;i<s;i++)
{
cs= cs+a[i];
if(cs<0)
{
cs=0;
}
ms=max(cs,ms);
}

return ms;

}

int main()
{
long long int a[10],n,t,max_sum[10];
cin>>t;
cin>>n;
for(int j=0;j<t;j++)
{
for(long long int i=0;i<n;i++)
{
cin>>a[i];
}
max_sum[j] = maxSubArraySum(a, n);
}
for(long long int i=0;i<t;i++)
{
cout<<max_sum[i]<<endl;
}
return 0;
}

@sharma.shubham You are not handling the multiple test cases correctly. You do not need to form an array of results when number of test cases are given.
Refer this code to check how to handle multiple test cases.

1 Like