what is this wrap thing and why are we inverting the array value
can u please elaborate soln
#include
using namespace std;
int kadane(int a[], int n);
int max_circular_sum(int a[], int n)
{
int max_kadane = kadane(a, n);
// Case 2: Now find the maximum sum that includes
// corner elements.
int max_wrap = 0, i;
for (i = 0; i < n; i++)
{
max_wrap += a[i]; // Calculate array-sum
a[i] = -a[i]; // invert the array (change sign)
}
// max sum with corner elements will be:
// array-sum - (-max subarray sum of inverted array)
max_wrap = max_wrap + kadane(a, n);
// The maximum circular sum will be maximum of two sums
return (max_wrap > max_kadane)? max_wrap: max_kadane;
}
int kadane(int a[], int n){
int cs=0;
int ms=0;
for(int i=0;i<n;i++){
cs+=a[i];
if(cs<0){
cs=0;
}
ms=max(cs,ms);
}
return ms;
}
int main() {
int t;
cin>>t;
for(int i=0;i<t;i++){
int n;
cin>>n;
int a[n];
for(int j=0;j<n;j++){
cin>>a[j];
}
cout<<max_circular_sum(a,n)<<endl;
}
return 0;
}
