#include<stdio.h>
#include<math.h>
int maxTreeSum(int a[], int size, int windowsize)
{
int max_so_far = 0, max_wind_start = -1;
int i,j,sum=0;
//int max_index[windowsize];
int w=windowsize;
int n=size;
for(i=0;i<n;i++){
for(j=0;j<w;j++){
sum=sum+a[(j+i)%n];
}
if(max_so_far < sum){ max_wind_start=i ;max_so_far = sum; }
sum=0;
}
printf("Max sum resulting window");
j=max_wind_start;
while(w--){
printf(" %d",a[j%n]);
j++;
}
printf("\n");
return max_so_far;
}
int main()
{
int a[] = {2, 3, 4, 1, 2, 1, 5, 3}; //fruit values
int n = sizeof(a)/sizeof(a[0]); // n
int s = 3;
/* As Bird has 3 seconds it can stay at node for 0.5 sec + 0.5 sec to go to
next node so bird can traverse 1 node in 1 sec
hence in 3 seconds bird can traverse 3 node*/
int max_sum = maxTreeSum(a, n, s);
printf("Maximum contiguous sum is %d\n", max_sum);
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8bWF0aC5oPgoKaW50IG1heFRyZWVTdW0oaW50IGFbXSwgaW50IHNpemUsIGludCB3aW5kb3dzaXplKQp7CiAgIGludCBtYXhfc29fZmFyID0gMCwgbWF4X3dpbmRfc3RhcnQgPSAtMTsKICAgaW50IGksaixzdW09MDsKICAgLy9pbnQgbWF4X2luZGV4W3dpbmRvd3NpemVdOwogICBpbnQgdz13aW5kb3dzaXplOwogICBpbnQgbj1zaXplOwogICBmb3IoaT0wO2k8bjtpKyspewogICAgICAgZm9yKGo9MDtqPHc7aisrKXsKICAgICAgICAgICBzdW09c3VtK2FbKGoraSklbl07CiAgICAgICB9CiAgICAgICBpZihtYXhfc29fZmFyIDwgc3VtKXsgbWF4X3dpbmRfc3RhcnQ9aSA7bWF4X3NvX2ZhciA9IHN1bTsgfQogICAgICAgc3VtPTA7CgogICB9CiAgIHByaW50ZigiTWF4IHN1bSByZXN1bHRpbmcgd2luZG93Iik7CiAgIGo9bWF4X3dpbmRfc3RhcnQ7CiAgIAogICB3aGlsZSh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