I wrote the solution to Maximum Circular Subarray Sum
#include
#include
using namespace std;
int main() {
//test cases
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int a[n],currentSum1=0,currentSum2=0,k1=0,k2=0;
for(int i=0;i<n;i++){
cin>>a[i];
currentSum1 = currentSum1 + a[i];
currentSum2 = currentSum2 + (-a[i]);
if(currentSum1<=0){
currentSum1 = 0;
}
if(currentSum1>=k1){
k1 = currentSum1;
}
if(currentSum2<=0){
currentSum2 = 0;
}
if(currentSum2>=k2){
k2 = currentSum2;
}
}
k2 = currentSum1 + k2;
//cout<<k1<<endl;
//cout<<k2<<endl;
cout<<max(k1,k2)<<endl;
}
return 0;
}
I am getting correct answer for the sample input given in the pdf.
I wanted to ask whether this problem is similar to this problem or not
https://hack.codingblocks.com/app/practice/2/p/1288
If it is similar, for the sample input given on this link it is giving me answer as 23 which must be 22.
Please help.