fork(2) download
  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4. int maxTreeSum(int a[], int size, int windowsize)
  5. {
  6. int max_so_far = 0, max_wind_start = -1;
  7. int i,j,sum=0;
  8. //int max_index[windowsize];
  9. int w=windowsize;
  10. int n=size;
  11. for(i=0;i<n;i++){
  12. for(j=0;j<w;j++){
  13. sum=sum+a[(j+i)%n];
  14. }
  15. if(max_so_far < sum){ max_wind_start=i ;max_so_far = sum; }
  16. sum=0;
  17.  
  18. }
  19. printf("Max sum resulting window");
  20. j=max_wind_start;
  21.  
  22. while(w--){
  23. printf(" %d",a[j%n]);
  24. j++;
  25. }
  26. printf("\n");
  27. return max_so_far;
  28. }
  29.  
  30. int main()
  31. {
  32. int a[] = {2, 3, 4, 1, 2, 1, 5, 3}; //fruit values
  33. int n = sizeof(a)/sizeof(a[0]); // n
  34. int s = 3;
  35. /* As Bird has 3 seconds it can stay at node for 0.5 sec + 0.5 sec to go to
  36.   next node so bird can traverse 1 node in 1 sec
  37.   hence in 3 seconds bird can traverse 3 node*/
  38. int max_sum = maxTreeSum(a, n, s);
  39. printf("Maximum contiguous sum is %d\n", max_sum);
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 3300KB
stdin
Standard input is empty
stdout
Max sum resulting window 5 3 2
Maximum contiguous sum is 10