Maximum circular subarray sum

Getting correct results on custom input, but failing all testcases

#include<bits/stdc++.h> using namespace std; int largest(int n, int a[]) { int maxsum=0,currsum=0,f=0,l=n,currb=0,index=-1; for(int i=0 ;i<l; ++i) {currb=currsum; currsum=currsum+a[i]; if(currsum<=0) { currsum=0; index=-1; } else { if(currb==0) {index=i; } if(currsum>maxsum) { maxsum=currsum; } } if(f==0&&i==(n-1)) { i=-1; f=1; if(index==-1) { l=n; index=n; } else {l=index;} } } return maxsum; } int main() { int a[100000]; int n, t,max; cin>>t; for(int i=0;i<t;++i) { cin>>n; for(int j=0;j<n;j++) { cin>>a[j]; } max=largest(n,a); cout<<max<<endl; } return 0; }

hi @shashank3256
for this input
8
10 -3 -4 7 6 5 -4 -1
8
-1 40 -14 7 6 5 -4 -1

your output is:
21
44

but correct output is
23
53
now consider these case also and try again

okay, can you explain any of the test cases you provided. Cuz, according to my logic, my program is giving right output. so i have understood something wrong,i guess.

8
10 -3 -4 7 6 5 -4 -1
8

8
-1 40 -14 7 6 5 -4 -1

the bold one are maximum circular subarray sum

i hope you get an clearity now

1 Like

thanksss! i get it now

1 Like