used kadane’s algo
#include<bits/stdc++.h>
using namespace std;
int main() {
int t=0;
cin>>t;
while(t>0)
{
int n=0;
cin>>n;
int a[1000]={0};
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int csum=0,maxsum=0;
for(int i=0;i<n;i++)
{
csum+=a[i];
if(csum<0) csum=0;
else if(csum>maxsum) maxsum=csum;
}
cout<<maxsum<<endl;
t–;
}
return 0;
}
Test cases are not passing
This logic will fail when all the elements of the array are negative, it will give 0 as output.
i corrected it now. its working on ide but again test case not passing. please have a look
increase size of the array according to constraints
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.