Statistics

Problem Statement for "AverageCandyLifetime"

Problem Statement

On January 1, 2007, a confectioner made several candies.

On the last day of each month she allows her children to eat several of those candies.

The lifetime of a candy is the number of days between January 1 and the day the candy is eaten, inclusive. For example, the lifetime of a candy eaten on January 31 is 31, and the lifetime of a candy eaten on December 31 is 365 (note that 2007 wasn't a leap year).

You are given a int[] eatenCandies, the i-th element of which is the number of candies eaten on the last day of the i-th month of 2007 (January is month 0, February is month 1, etc.). Return the average lifetime of the candies.

Definition

Class:
AverageCandyLifetime
Method:
getAverage
Parameters:
int[]
Returns:
double
Method signature:
double getAverage(int[] eatenCandies)
(be sure your method is public)

Notes

  • The year 2007 was not a leap year.
  • The number of days in the months of 2007, in order, were 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 and 31.
  • The returned value must be accurate to within a relative or absolute value of 1E-9.

Constraints

  • eatenCandies will contain exactly 12 elements.
  • Each element of eatenCandies will be between 0 and 1000, inclusive.
  • The sum of all the elements in eatenCandies will be greater than 0.

Examples

  1. {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}

    Returns: 60.5

    One candy was eaten on January 31 and the other was eaten on March 31. The lifetimes of the candies are 31 and 31+28+31=90. The average lifetime is (31+90)/2=60.5.

  2. {0, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

    Returns: 59.0

    All candies were eaten on February 28. The lifetime of each candy is 31+28=59, so the average candy lifetime is 59.0.

  3. {0, 0, 0, 0, 0, 1, 0, 0, 0, 50, 0, 0}

    Returns: 301.5882352941176

    Most of the candies were eaten on October 31 (Halloween), and the lifetime of each of those candies is 304. The average lifetime is smaller than 304, because of a candy with lifetime 181, eaten on June 30.

  4. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1000}

    Returns: 365.0

    Eating all candies on the New Year's Eve makes the maximum possible average lifetime.

  5. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

    Returns: 252.80769230769232

  6. {902, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

    Returns: 31.0

  7. {0, 914, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

    Returns: 59.0

  8. {0, 0, 576, 0, 0, 0, 0, 0, 0, 0, 0, 0}

    Returns: 90.0

  9. {0, 0, 0, 369, 0, 0, 0, 0, 0, 0, 0, 0}

    Returns: 120.0

  10. {0, 0, 0, 0, 996, 0, 0, 0, 0, 0, 0, 0}

    Returns: 151.0

  11. {0, 0, 0, 0, 0, 135, 0, 0, 0, 0, 0, 0}

    Returns: 181.0

  12. {0, 0, 0, 0, 0, 0, 741, 0, 0, 0, 0, 0}

    Returns: 212.0

  13. {0, 0, 0, 0, 0, 0, 0, 736, 0, 0, 0, 0}

    Returns: 243.0

  14. {0, 0, 0, 0, 0, 0, 0, 0, 405, 0, 0, 0}

    Returns: 273.0

  15. {0, 0, 0, 0, 0, 0, 0, 0, 0, 239, 0, 0}

    Returns: 304.0

  16. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 385, 0}

    Returns: 334.0

  17. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 687}

    Returns: 365.0

  18. {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000}

    Returns: 196.91666666666666

  19. {1000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

    Returns: 31.027972027972027

  20. {0, 8, 33, 74, 132, 207, 298, 405, 529, 669, 826, 1000}

    Returns: 291.79359005022724

  21. {0, 0, 0, 0, 0, 0, 2, 11, 41, 134, 386, 1000}

    Returns: 348.76111817026685

  22. {0, 91, 181, 269, 356, 439, 519, 594, 665, 730, 789, 841}

    Returns: 258.5823894775301

  23. {1000, 245, 880, 676, 549, 945, 86, 987, 397, 793, 785, 408}

    Returns: 190.7308734356857

  24. {492, 547, 598, 647, 693, 0, 87, 167, 241, 310, 375, 435}

    Returns: 171.3220818815331

  25. {928, 967, 920, 937, 966, 938, 957, 999, 937, 951, 942, 924}

    Returns: 197.00325532289284

  26. {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

    Returns: 31.0

  27. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

    Returns: 59.0

  28. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}

    Returns: 334.0

  29. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}

    Returns: 365.0

  30. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

    Returns: 59.0

  31. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }

    Returns: 196.91666666666666

  32. {1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

    Returns: 54.333333333333336

  33. {2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

    Returns: 54.0

  34. {0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

    Returns: 90.0

  35. {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

    Returns: 60.5

  36. {1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 }

    Returns: 196.91666666666666

  37. {1, 2, 3, 4, 5, 6, 3, 3, 4, 2, 3, 4 }

    Returns: 206.675

  38. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }

    Returns: 252.80769230769232

  39. {555, 354, 485, 669, 118, 211, 669, 336, 999, 1000, 561, 972 }

    Returns: 225.19656516091788

  40. {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100 }

    Returns: 361.6930693069307


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: