hey code is giving wrong answer for
5
-1 -4 -5 -2 -1
Can you tell why??
Maximum circular sum
hey @Dhruv-Miglani-2090236747707980, your code is giving right output for this case, expected output is zero…
no the answer should be -1 in this case .
Even the code is not getting submitted on geeksforgeeks compiler and showing error in the above test case
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.
#include
using namespace std;
int kadane(int a[], int n)
{
int currentsum=0;
int maxsum=0;
for(int i=0;i<n;i++)
{
currentsum+=a[i];
if(currentsum<0)
{
currentsum=0;
}
maxsum=max(currentsum,maxsum);
}
return maxsum;
}
int circular(int a[],int n)
{
int c1=kadane(a,n);
int c2=0;
for(int i=0;i<n;i++)
{
c2+=a[i];
a[i]=-a[i];
}
c2=c2+kadane(a,n);
return (c2>c1)?c2:c1;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++){
int a[1000];
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<circular(a,n);
}
return 0;
}
whats the error in this code