#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n,a[1000];
cin>>t;
if(1<=t<=100)
{
while(t>0)
{
int sum=0,mx=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
sum=sum+a[i];
if(sum<0)
{
sum=0;
}
mx=max(sum,mx);
}
for(int i=0;i<n-1;i++)
{
sum=sum+a[i];
if(sum<0)
{
sum=0;
}
mx=max(sum,mx);
}
cout<<mx<<endl;
t–;
}
}
return 0;
}
I don't understand this program
Hey, can you please tell me the souce of this code?
For maximum circular sum what you can do is first find maximum using Kadane algo(This will handle the case when maximum sum in circular array is same as that in non-circular array).
Then find the maximum subarray sum of inverted array( array with sign changed) using kadane algo.Subtract it from total sum and return maximum of value obtained from first kadane and total sum - Kadane of second. It works as to get the maximum sum you can easily remove a central portion of minimum sum and then get an array of first to start index of the portion removed to ending index of portion removed to last index.Read more in the link attached below.
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.