Can you help me in understanding what is wrong with my logic for the problem? I am rotating the array and comparing the maximum sum of the subarrays.
include
include
using namespace std ;
int max_subarray(int v[],int n){
int ms = v[0] ;
int cs = v[0] ;
for(int i = 1; i < n;i++)
{
if(cs < 0) cs = 0 ;
cs += v[i] ;
ms = max(ms,cs) ;
}
return ms ;
}
int main() {
int t ;
cin >> t ;
while(t–){
int n;
cin >> n ;
int a[n] ;
for(int i = 0; i < n; i++){
cin >> a[i] ;
}
int max = max_subarray(a,n) ;
for(int i = 0; i < n; i++){
rotate(a,a+i,a+n) ;
if(max < max_subarray(a,n) ) max = max_subarray(a,n) ;
}
cout << max << endl ;
}
return 0;
}